개발/백준
[백준] 1003번. 피보나치 함수 (C++)
yun000
2023. 12. 27. 12:27
문제 설명
https://www.acmicpc.net/problem/1003
1003번: 피보나치 함수
각 테스트 케이스마다 0이 출력되는 횟수와 1이 출력되는 횟수를 공백으로 구분해서 출력한다.
www.acmicpc.net
해결 방법
피보나치 수열은 숫자 2개를 더해서 그 다음 숫자를 만드는 것이다.
0 1 1 2 3 5 8 13.....
그 말은 즉 0과 1의 개수도 같은 방식으로 구할 수 있다는 것이다.
+ 출력할 때 뒤에 "\n"을 꼭 붙이자. 저거 안붙여서 틀렸음 ㅡ..ㅡ
#include<iostream>
#include<vector>
using namespace std;
int main()
{
int t,n; cin >> t;
vector<pair<int, int>> count(41);
count[0].first = 1; count[0].second = 0;
count[1].first = 0; count[1].second = 1;
count[2].first = 1; count[2].second = 1;
for (int i = 3; i <= 40; i++)
{
count[i].first = count[i - 1].first + count[i - 2].first;
count[i].second = count[i - 1].second + count[i - 2].second;
}
for (int j = 0; j < t; j++)
{
cin >> n;
cout << count[n].first << " " << count[n].second << "\n";
}
}