https://www.acmicpc.net/problem/9095
문제 정수 4를 1, 2, 3의 합으로 나타내는 방법은 총 7가지가 있다. 합을 나타낼 때는 수를 1개 이상 사용해야 한다. 1+1+1+1 1+1+2 1+2+1 2+1+1 2+2 1+3 3+1 정수 n이 주어졌을 때, n을 1, 2, 3의 합으로 나타내는 방법의 수를 구하는 프로그램을 작성하시오. 입력 첫째 줄에 테스트 케이스의 개수 T가 주어진다. 각 테스트 케이스는 한 줄로 이루어져 있고, 정수 n이 주어진다. n은 양수이며 11보다 작다. |
code>>
<<DFS>>
#include <iostream>
int Solv(const int& sum, const int& index, const int& max)
{
if (sum == max)
return true;
if (index > max || sum > max)
return false;
int result = 0;
for(int i= 1; i<= 3; i++)
result += Solv(sum + i, i, max);
return result;
}
int main()
{
std::ios::ios_base::sync_with_stdio(false);
int t;
std::cin >> t;
int input = 0;
while (t-- > 0)
{
std::cin >> input;
int count = 0;
count += Solv(0, 1, input);
printf("%d\n", count);
}
return 0;
}
<<DP>>
#include <iostream>
int memo[11] = { 1 ,2, 4 };
int main()
{
std::ios::ios_base::sync_with_stdio(false);
int t, input;
std::cin >> t;
while (t-- > 0)
{
std::cin >> input;
for (int i = 3; i < input; i++)
memo[i] = memo[i - 1] + memo[i - 2] + memo[i - 3];
printf("%d\n", memo[input - 1]);
}
return 0;
}
'알고리즘 > 백준' 카테고리의 다른 글
3665>최종순위 (0) | 2018.10.20 |
---|---|
2252>줄 세우기 (0) | 2018.10.15 |
10216>CountCircleGroups (0) | 2018.10.12 |
1004>어린왕자 (0) | 2018.09.23 |
1005>ACM Craft (0) | 2018.09.20 |