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
32
33
34
35
36
37
38
39
40
41
42
43
44
|
#include <iostream>
#include <vector>
using namespace std;
vector<long long> cache(91, -1);
long long fibo(int n) {
if (n <= 1) {
cache[n] = n;
return n;
}
long long& ret = cache[n];
if (ret != -1) {
return ret;
}
return ret = fibo(n - 2) + fibo(n - 1);
}
int main() {
int t;
cin >> t;
for (int tc = 0; tc < t; tc++) {
int n;
cin >> n;
fibo(n);
if (n == 0) {
cout << "1 0\n";
}
else if (n == 1) {
cout << "0 1\n";
}
else {
cout << cache[n - 1] << " " << cache[n] << "\n";
}
}
return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
|
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs |
피보나치 수2 할때 한 코드에서 고침
피보나치수를 구할때 fibo(1)과 fibo(0) 호출 횟수에는 규칙이 있음
fibo(0)의 호출 횟수 == fibo(n-1)
fibo(1)의 호출 횟수 == fibo(1)
'Baekjoon' 카테고리의 다른 글
[#1149] RGB거리 (0) | 2020.03.10 |
---|---|
[#9461] 파도반 수열 (0) | 2020.03.10 |
[#2748] 피보나치 수 2 (0) | 2020.03.10 |
[#14889] 스타트와 링크 (0) | 2020.03.10 |
[#14888] 연산자 끼워넣기 (0) | 2020.03.10 |