Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int fib(int x) {
- if(x == 1) {
- return 1;
- }
- if(x == 2) {
- return 1;
- }
- return fib(x - 1) + fib(x - 2);
- }
- int main()
- {
- int n;
- cin >> n;
- cout << fib(n) << endl;
- return 0;
- }
- // fib(7) --> fib(6) + fib(5) = 8 + 5 = 13
- // fib(6) --> fib(5) + fib(4) = 5 + 3 = 8
- // fib(5) --> fib(4) + fib(3) = 3 + 2 = 5
- // fib(4) --> fib(3) + fib(2) = 2 + 1 = 3
- // fib(3) --> fib(2) + fib(1) = 1 + 1 = 2
- // fib(2) --> 1
- // fib(1) --> 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement