- Notifications
You must be signed in to change notification settings - Fork 845
/
Copy path2.cpp
25 lines (21 loc) · 715 Bytes
/
2.cpp
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
#include<bits/stdc++.h>
usingnamespacestd;
// 한 번 계산된 결과를 메모이제이션(Memoization)하기 위한 배열 초기화
longlong d[100];
// 피보나치 함수(Fibonacci Function)를 재귀함수로 구현 (탑다운 다이나믹 프로그래밍)
longlongfibo(int x) {
// 종료 조건(1 혹은 2일 때 1을 반환)
if (x == 1 || x == 2) {
return1;
}
// 이미 계산한 적 있는 문제라면 그대로 반환
if (d[x] != 0) {
return d[x];
}
// 아직 계산하지 않은 문제라면 점화식에 따라서 피보나치 결과 반환
d[x] = fibo(x - 1) + fibo(x - 2);
return d[x];
}
intmain(void) {
cout << fibo(50) << '\n';
}