Advertisement
Josif_tepe

Untitled

Jul 22nd, 2023
1,130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.67 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cstring>
  4. #include <vector>
  5. #include <set>
  6. using namespace std;
  7.  
  8. /*
  9.  da se napise programa koja ke gi sobere cifrite na eden broj
  10.  rekurzivno
  11.  **/
  12. int dp[1005];
  13. int fib(int n) {
  14.     if(n <= 2) {
  15.         return 1;
  16.     }
  17.     if(dp[n] != -1) {
  18.         return dp[n];
  19.     }
  20.     return dp[n] = fib(n - 1) + fib(n - 2);
  21. }
  22. int main() {
  23.     ios::sync_with_stdio(false);
  24.     memset(dp, -1, sizeof dp);
  25.     cout << fib(5) << endl;
  26.     return 0;
  27. }
  28. // fib(5) = fib(4) + [fib(3)] = 3 + 2 = 5
  29. // fib(4) = fib(3) + fib(2) = 2 + 1 = 3
  30. // fib(3) = fib(2) + fib(1) = 2
  31. // fib(2) = 1
  32. // fib(1) = 1
  33. // fib(2) = 1
  34. // fib(3) = 2
  35.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement