Advertisement
Josif_tepe

Untitled

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