티스토리 뷰
https://www.acmicpc.net/problem/9009
9009번: 피보나치
입력 데이터는 표준입력을 사용한다. 입력은 T 개의 테스트 데이터로 구성된다. 입력의 첫 번째 줄에는 테스트 데이터의 수를 나타내는 정수 T 가 주어진다. 각 테스트 데이터에는 하나의 정수 n
www.acmicpc.net
1. 정수 n이 포함되는 피보나치 수열을 구한다
2. 찾은 피보나치 수열을 역순으로 돌면서 n과 제일 가까우면서 작은수를 구한다.
n에서 찾은수를 뺀값의 제일 가까우면서 작은수를 구한다. 반복
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
int tc;
cin >> tc;
while(tc--)
{
int n;
cin >> n;
vector<int> pibo;
pibo.push_back(0);
pibo.push_back(1);
// 피보나치 수열 생성
int num = 1;
while(num <= n)
{
num = pibo[pibo.size()-2] + pibo[pibo.size()-1];
pibo.push_back(num);
}
// 답 찾음
vector<int> ans;
int target = n;
for(int i = pibo.size()-1; i >= 0; i--)
{
if(pibo[i] <= target)
{
ans.push_back(pibo[i]);
target = target - pibo[i];
}
if(target == 0)
break;
}
sort(ans.begin(), ans.end());
for(auto x : ans)
cout << x << ' ';
cout << '\n';
}
}
'PS' 카테고리의 다른 글
백준 19941. 햄버거 분배 (0) | 2021.08.17 |
---|---|
백준 18310. 안테나 (0) | 2021.08.17 |
백준 2720. 세탁소 사장 동혁 (0) | 2021.08.16 |
백준 2810. 컵홀더 (0) | 2021.08.16 |
백준 2828. 사과 담기 게임 (0) | 2021.08.15 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- Tree
- 이분탐색
- BFS
- Spring
- Implementation
- 자료구조
- two pointer
- Brute Force
- db
- MVC
- greedy
- back tracking
- Dijkstra
- permutation
- C
- Stack
- C++
- dfs
- DP
- graph
- Kruskal
- 조합
- Unity
- recursion
- CSS
- Python
- priority queue
- binary search
- floyd warshall
- 재귀
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함