#include <iostream> #include <math.h> using namespace std; int max_mul; int count_mul(int n) { return log(n) / log(3); } int solve(int add, int mul, int n) { int result = 0; // '*'의 최대 갯수보다 '+'의 갯수가 많아지면 종료 if(2 * max_mul < add) { return 0; } if(n == 1) { return 1; } if(n % 3 == 0) { if(2 * (mul + 1) <= add) { result += solve(add, mul + 1, n / 3); } result += solve(add + 3, mul, n - 3); } else if(n % 3 != 0) { result += solve(add + (n % 3), mul, n - (n % 3)); } return result; } // 전역 변수를 정의할 경우 함수 내에 초기화 코드를 꼭 작성해주세요. int solution(int n) { int answer = 0; max_mul = count_mul(n); answer = solve(0, 0, n); return answer; } | cs |
'Programmers > Level 4' 카테고리의 다른 글
[프로그래머스 4] 징검다리 (C/C++) (★★★) (0) | 2020.04.26 |
---|---|
[프로그래머스 4] 블록 게임 (C/C++) (★★) (0) | 2020.03.05 |
[프로그래머스 4] 카드게임 (C/C++) (★★) (0) | 2020.01.15 |
[프로그래머스 4] 도둑질 (C/C++) (★) (0) | 2020.01.15 |