Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <queue>
- #include <cmath>
- using namespace std;
- const int maxn = 100;
- int dp[maxn];
- int fib(int n) {
- if(n == 1 or n == 2) {
- return 1;
- }
- if(dp[n] != -1) {
- return dp[n];
- }
- return dp[n] = fib(n - 1) + fib(n - 2);
- }
- int main() {
- for(int i = 0; i < maxn; i++) {
- dp[i] = -1;
- }
- int n;
- cin >> n;
- cout << fib(n) << endl;
- return 0;
- }
- // fib(4) = fib(3) + fib(2) = 2 + 1 = 3
- // fib(3) = fib(2) + fib(1) = 1 + 1 = 2
- // fib(2) = 1
- // fib(1) = 1
- // fib(2) = 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement