Recursive Fibonacci

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

    int fib(int n) {
        if (n == 1 || n == 2) {
            return 1;
        } else {
            return fib(n - 1) + fib(n - 2);
        }
    }
    

    in fibonacci sequence first 2 numbers always sequels to 1 then every time the value became 1 or 2 it must return 1

提交回复
热议问题