Fibonacci sequence f(n

Title description The
Fibonacci sequence f(n) satisfies the following definition:

f(0) = 1, f(1) = 1, f(n) = f(n-1) + f(n-2) (n >= 2)。

Please use the recursive method to write the function, for a given n, find the nth term f(n) of the Fibonacci sequence

Enter description
Enter an integer n per line

0 <= n<= 30

Output description
For each line of input, output the value of the nth term of the Fibonacci sequence f(n)

#include<iostream>

using namespace std;


int fun(int n)
{
    
    
    if(n == 1 || n == 2)
        return 1;
    return fun(n - 1) + fun(n - 2);
}
int main()
{
    
    
    int a;
    while(cin>>a)
    {
    
    
        cout<<fun(a)<<endl;
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/KO812605128/article/details/115205710