[๋ฐฑ์ค(BOJ)] 9095๋ฒ: 1, 2, 3 ๋ํ๊ธฐ (Dynamic Programming, C++)
1 = 1 (1๊ฐ์ง) DP[0] = 1
2 = 1 + 1 = 2 (2๊ฐ์ง) DP[1] = 2
3 = 1 + 1 + 1 = 1 + 2 = 2 + 1 = 3 (4๊ฐ์ง) DP[2] = 4
4 = 1 + 1 + 1 + 1 = 1 + 1 + 2 = 1 + 2 + 1 = 1 + 3 = 2 + 1 + 1 = 2 + 2 = 3 + 1 (7๊ฐ์ง) DP[3] = 7
๋ฐ๋ผ์ ์ ํ์์ DP[i] = DP[i-3] + DP[i-2] + DP[i-1]
#include <stdio.h>
#include <vector>
using namespace std;
int main(){
int T;
scanf("%d", &T);
for(int i=0; i<T; i++){
int n;
scanf("%d", &n);
vector<int> DP(n);
DP[0] = 1; DP[1] = 2; DP[2] = 4;
for(int j=3; j<n; j++){
DP[j] = DP[j-2] + DP[j-1] + DP[j-3];
}
printf("%d\n", DP[n-1]);
}
}
[๋ฐฑ์ค(BOJ)] 1924๋ฒ: 2007๋ (์ ์ถ๋ ฅ, C++) (0) | 2021.02.11 |
---|---|
[๋ฐฑ์ค(BOJ)] 2739๋ฒ: ๊ตฌ๊ตฌ๋จ (์ ์ถ๋ ฅ, C++) (0) | 2021.02.11 |
[๋ฐฑ์ค(BOJ)] 11727๋ฒ: 2รn ํ์ผ๋ง 2 (Dynamic Programming, C++) (0) | 2021.02.11 |
[๋ฐฑ์ค(BOJ)] 11726๋ฒ: 2รn ํ์ผ๋ง (Dynamic Programming, C++) (0) | 2021.02.11 |
[๋ฐฑ์ค(BOJ)] 1463๋ฒ: 1๋ก ๋ง๋ค๊ธฐ (Dynamic Programming, C++) (0) | 2021.02.11 |