Recursive Fibonacci

前端 未结 13 1678
情歌与酒
情歌与酒 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:40

    My solution is:

    #include 
    
    
        int fib(int number);
    
        void call_fib(void);
    
        int main()
        {
        call_fib();
        return 0;
        }
    
        void call_fib(void)
        {
          int input;
          std::cout<<"enter a number\t";
          std::cin>> input;
          if (input <0)
          {
            input=0;
            std::cout<<"that is not a valid input\n"   ;
            call_fib();
         }
         else 
         {
             std::cout<<"the "<0)
       {
            return fib(x-1)+fib(x-2);
        }
        else 
         return -1;
        }
    

    it returns fib(0)=0 and error if negitive

提交回复
热议问题