求斐波那契數列前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;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM