递归法求斐波那契数列

#include <iostream>
using namespace std;

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

int main()
{
    int n;
    cout << "请输入正整数n:";
    cin >> n;
    cout << "斐波那契数列的前n项为:" << endl;
    
    for (int t = 1; t <= n; t++)
    {
        cout << Fibonacci(t) << '\t';
    }

    cout << endl;

    return 0;
}

 

上一篇:递归


下一篇:欧拉计划002--偶斐波那契数