Advertisement
Josif_tepe

Untitled

Nov 20th, 2024
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.60 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4. #include <cmath>
  5. using namespace std;
  6. const int maxn = 100;
  7.  
  8. int dp[maxn];
  9. int fib(int n) {
  10.     if(n == 1 or n == 2) {
  11.         return 1;
  12.     }
  13.     if(dp[n] != -1) {
  14.         return dp[n];
  15.     }
  16.    
  17.    
  18.     return dp[n] = fib(n - 1) + fib(n - 2);
  19. }
  20. int main() {
  21.     for(int i = 0; i < maxn; i++) {
  22.         dp[i] = -1;
  23.     }
  24.    
  25.     int n;
  26.     cin >> n;
  27.     cout << fib(n) << endl;
  28.    
  29.     return 0;
  30. }
  31. // fib(4) = fib(3) + fib(2) = 2 + 1 = 3
  32. // fib(3) = fib(2) + fib(1) = 1 + 1 = 2
  33. // fib(2) = 1
  34. // fib(1) = 1
  35. // fib(2) = 1
  36.  
  37.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement