Advertisement
Josif_tepe

Untitled

Oct 18th, 2021
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.42 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. int memo[105];
  5. int fib(int n) {
  6.     if(n <= 2) {
  7.         return 1;
  8.     }
  9.     if(memo[n] != -1) {
  10.         return memo[n];
  11.     }
  12.     return memo[n] = fib(n - 1) + fib(n - 2);
  13. }
  14. int main() {
  15.     int n;
  16.     cin >> n;
  17.     for(int i = 0; i <= n; i++) {
  18.         memo[i] = -1;
  19.     }
  20.     cout << fib(n) << endl;
  21.     return 0;
  22. }
  23. // fib(7) = fib(6) + fib(5)
  24. //
  25.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement