-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFibonacci_Number.cpp
More file actions
45 lines (40 loc) · 882 Bytes
/
Fibonacci_Number.cpp
File metadata and controls
45 lines (40 loc) · 882 Bytes
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
45
#include <bits/stdc++.h>
using namespace std;
#define MAX 44
int Fib[MAX];
// n番目のフィボナッチ数列を求める(再帰)
// int Calc_Fibonacci(int n1, int n2, int cnt)
// {
// cnt--;
// int n3 = n1 + n2;
// if(cnt == 0) {
// return n3;
// } else {
// return Calc_Fibonacci(n2, n3, cnt);
// }
// }
// n番目のフィボナッチ数列を求める(動的計画法)
int MakeFibonacci(int n)
{
Fib[0] = Fib[1] = 1;
for (int i = 2; i <= n; i++)
{
Fib[i] = Fib[i-1] + Fib[i-2];
}
return Fib[n];
}
int main()
{
int n = 0;
int nRes = 0;
scanf("%d",&n);
// 再帰用
// if (n==0 || n==1) {
// printf("1\n");
// } else {
// nRes = Calc_Fibonacci(1, 1, n-1);
// printf("%d\n", nRes);
// }
nRes = MakeFibonacci(n);
printf("%d\n", nRes);
}