Advertisement
Josif_tepe

Untitled

Feb 18th, 2025
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.51 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3. #include <cmath>
  4. #include <vector>
  5. using namespace std;
  6.  
  7. int rec(int x) {
  8.     if(x <= 2) {
  9.         return 1;
  10.     }
  11.    
  12.     return rec(x - 1) + rec(x - 2);
  13. }
  14. int main()
  15. {
  16.    
  17.     cout << rec(5) << endl;
  18.  
  19.     return 0;
  20. }
  21.  
  22. // rec(5) = rec(4) +  rec(3)  = 3 + 2 = 5
  23. // rec(4) = rec(3) + rec(2) = 2 + 1 = 3
  24. // rec(3) = rec(2) + rec(1) = 1 + 1 = 2
  25. // rec(2) = 1
  26. // rec(1) = 1
  27. // rec(2) = 2
  28. // rec(3) = rec(2) + rec(1) = 1 + 1 = 2
  29. // rec(2) = 1
  30. // rec(1) = 1
  31.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement