Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | ||||
| 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 11 | 12 | 13 | 14 | 15 | 16 | 17 |
| 18 | 19 | 20 | 21 | 22 | 23 | 24 |
| 25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 북리뷰
- c++
- Stack
- 정석
- 오퍼레이팅시스템
- 컴공
- 코딩
- 그래프
- 너비우선탐색
- cs
- 오에스
- OS
- 브루트포스
- Computer science
- 컴퓨터공학과
- Operating System
- vector
- 자료구조
- 문제풀이
- 알고리즘
- 정석학술정보관
- 개발
- 코테
- 컴공과
- coding
- bfs
- 구현
- DP
- 백준
- 스택
Archives
- Today
- Total
Little Jay
[C++] 백준 9009번 - 피보나치 본문
피보나치를 이용한 재귀 문제였다.
Zeckendorf's theorem을 사용하는 문제였는데, 이분의 블로그를 보면 쉽게 이해가 가능하다.
결과적으로 피보나치 수가 n 보다 클 때 직전항을 빼주면서 재귀적으로 내려가면 된다.
#include <iostream>
using namespace std;
long long fibo[10000001];
void solve(int n) {
if (n == 0)
return;
for (int i = 0; i <= 10000000; i++) {
if (n < fibo[i]) {
solve(n - fibo[i - 1]);
cout << fibo[i - 1] << " ";
return;
}
}
return;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t, n;
cin >> t;
fibo[1] = 1;
for (int i = 2; i <= 10000000; i++) {
fibo[i] = fibo[i - 1] + fibo[i - 2];
}
while (t--) {
cin >> n;
solve(n);
cout << endl;
}
return 0;
}'알고리즘 > BOJ' 카테고리의 다른 글
| [C++] 백준 20301번 - 반전 요세푸스 (0) | 2022.08.05 |
|---|---|
| [C++] 백준 13273번 - 로마숫자 (0) | 2022.07.30 |
| [C++] 백준 5972번 - 택배 배송 (0) | 2022.07.22 |
| [C++] 백준 11559번 - Puyo Puyo (0) | 2022.07.21 |
| [C++] 백준 2174 - 로봇 시뮬레이션 (0) | 2022.07.19 |
Comments