Baekjoon/DP
[백준 1003] 피보나치 함수 (DP) (C/C++)
워니-
2020. 3. 15. 15:27
#include <iostream> #include <algorithm> #include <string.h> using namespace std; int T, N; int dp0[45]; int dp1[45]; int solve0(int n) { if(n == 0) { return 1; } else if(n == 1) { return 0; } if(dp0[n] != 0) { return dp0[n]; } dp0[n] = solve0(n-1) + solve0(n-2); return dp0[n]; } int solve1(int n) { cout << n << endl; if(n == 0) { return 0; } else if(n == 1) { return 1; } if(dp1[n] != 0) { return dp1[n]; } dp1[n] = solve1(n-1) + solve1(n-2); return dp1[n]; } int main(void) { // freopen("B1003_input.txt", "r", stdin); cin >> T; while(T--) { cin >> N; cout << solve0(N) << " " << solve1(N) << endl; } return 0; } | cs |