Recursive Fibonacci

前端 未结 13 1628
情歌与酒
情歌与酒 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:50
    if(n==1 || n==0){
        return n;
    }else{     
        return fib(n-1) + fib(n-2);
    }
    

    However, using recursion to get fibonacci number is bad practice, because function is called about 8.5 times than received number. E.g. to get fibonacci number of 30 (1346269) - function is called 7049122 times!

    0 讨论(0)
提交回复
热议问题