-
Notifications
You must be signed in to change notification settings - Fork 326
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #419 from codelord09/main
Created Fibonacci Series using recursive(DP)
- Loading branch information
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
//Top-Down Approch ( Recursive approach) | ||
|
||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
#define mx 1000000 | ||
|
||
int dp[mx + 1]; | ||
|
||
int fib(int n) | ||
{ | ||
if (n == 0 || n == 1) | ||
return n; | ||
|
||
if (dp[n] != -1) | ||
{ | ||
return dp[n]; | ||
} | ||
|
||
int res; | ||
|
||
res = fib(n - 1) + fib(n - 2); | ||
|
||
dp[n] = res; | ||
|
||
return dp[n]; | ||
} | ||
|
||
int main() | ||
{ | ||
int t; | ||
cin >> t; | ||
while (t--) | ||
{ | ||
int n; | ||
cin >> n; | ||
for (int i = 1; i <= mx; ++i) | ||
{ | ||
dp[i] = -1; | ||
} | ||
cout << fib(n) << endl; | ||
} | ||
return 0; | ||
} | ||
|