2020-04-18 10:43:43 +08:00
|
|
|
#include <iostream>
|
2016-11-25 18:36:34 +05:30
|
|
|
using namespace std;
|
|
|
|
int arr[1000000];
|
2020-05-30 04:02:09 +00:00
|
|
|
int fib(int n) {
|
|
|
|
if (arr[n] == -1) {
|
2020-05-29 23:26:30 +00:00
|
|
|
if (n <= 1)
|
|
|
|
arr[n] = n;
|
|
|
|
else
|
|
|
|
arr[n] = fib(n - 1) + fib(n - 2);
|
|
|
|
}
|
|
|
|
return arr[n];
|
2016-11-25 18:36:34 +05:30
|
|
|
}
|
2020-05-30 04:02:09 +00:00
|
|
|
int main(int argc, char const *argv[]) {
|
2020-05-29 23:26:30 +00:00
|
|
|
int n;
|
|
|
|
cout << "Enter n: ";
|
|
|
|
cin >> n;
|
2020-05-30 04:02:09 +00:00
|
|
|
for (int i = 0; i < n + 1; ++i) {
|
2020-05-29 23:26:30 +00:00
|
|
|
arr[i] = -1;
|
|
|
|
}
|
|
|
|
cout << "Fibonacci number is " << fib(n) << endl;
|
|
|
|
return 0;
|
2016-11-25 18:36:34 +05:30
|
|
|
}
|