Advertisement
Josif_tepe

Untitled

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