Algorithm/dp
백준 1003 피보나치 함수 c++ // dp
Mini_96
2023. 10. 10. 01:39
https://www.acmicpc.net/problem/1003
1003번: 피보나치 함수
각 테스트 케이스마다 0이 출력되는 횟수와 1이 출력되는 횟수를 공백으로 구분해서 출력한다.
www.acmicpc.net
1. 전체코드
#include <bits/stdc++.h>
using namespace std;
int t, n;
int d[44][2];
//1.테이블정의
// d[i][0] : fibo(i)에서 0이 출력되는 횟수
// d[i][1] : fibo(i)에서 1이 출력되는 횟수
int fibonacci(int n) {
if (n == 0) {
printf("0");
return 0;
}
else if (n == 1) {
printf("1");
return 1;
}
else {
return fibonacci(n - 1) + fibonacci(n - 2);
}
}
int main(void) {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> t;
//2.초기값
d[0][0] = 1;
d[0][1] = 0;
d[1][0] = 0;
d[1][1] = 1;
d[2][0] = d[1][0] + d[0][0];
d[2][1] = d[1][1] + d[0][1];
//3. 점화식 for문
for (int i = 2; i <= 40; ++i) {
d[i][0] = d[i - 1][0] + d[i - 2][0];
d[i][1] = d[i - 1][1] + d[i - 2][1];
}
while (t--) {
cin >> n;
cout << d[n][0] << " " << d[n][1] << "\n";
}
}