Recursive Fibonacci

前端 未结 13 1657
情歌与酒
情歌与酒 2020-12-07 18:52

I\'m having a hard time understanding why

#include 

using namespace std;

int fib(int x) {
    if (x == 1) {
        return 1;
    } else {
         


        
13条回答
  •  离开以前
    2020-12-07 19:36

    This is my solution to fibonacci problem with recursion.

    #include 
    using namespace std;
    
    int fibonacci(int n){
        if(n<=0)
            return 0;
        else if(n==1 || n==2)
            return 1;
        else
            return (fibonacci(n-1)+fibonacci(n-2));
    }
    
    int main() {
        cout << fibonacci(8);
        return 0;
    }
    

提交回复
热议问题