代码

先贴代码为敬

#include <iostream>
using namespace std;
int main(){
    int n = 1;
    double n_m = 93;
    cout << endl;
    while(1){
        int n = 1;
        long long int a=1;
        long long int b=1;
        cout << "Please input the max value of n." << endl;
        cin >> n_m;
        int n_max = static_cast<int> (n_m);
        if (n_max < 1){
            cout << "The max value of n is error." << endl;
        }
        if (n_max > 92){  //因为斐波那契数列发散很快,所以给n一个上限,防止数列数值溢出
            cout << "The value is too large." << endl;
        }
        else{
            while(n <= n_max){
                if (n == 1){
                    cout << a << endl;
                        n++;
                    }
                    if ( n== 2){
                        cout << b << endl;
                    n++;
                    }
                else{
                    a = a+b;
                    cout << a << endl;
                        n++;
                    b = a+b;
                    cout << b << endl;
                        n++;
                }
            }
        }
    }
}

相关知识

斐波那契数列递推关系F(1)=1,F(2)=1, F(n)=F(n - 1)+F(n - 2)(n ≥ 3,n ∈ N*)
通项公式

为便于理解,本文章方法是根据递推关系来写的程序
相关数学知识请参考:百度百科

最后修改:2022 年 04 月 01 日
如果觉得我的文章对你有用,请随意赞赏