其他分享
首页 > 其他分享> > 求斐波那契数列前n项(循环结构)

求斐波那契数列前n项(循环结构)

作者:互联网

#include <iostream>
using namespace std;

void Fibonacci(int n)
{
    unsigned int a = 1, b = 1;
    cout << a << '\t' << b << '\t';
    for (int t = 3; t <= n; ++t)
    {
        b = a + b;
        a = b - a;
        cout << b << '\t';
    }
}

int main()
{
    unsigned int n;
    cout << "请输入正整数n:";
    cin >> n;
    cout << "斐波那契数列的前n项为:" << endl;
    Fibonacci(n);
    cout << endl;

    return 0;
}

 

标签:std,求斐波,数列,int,void,namespace,那契,cout
来源: https://www.cnblogs.com/XueQun/p/15291090.html