#include <iostream>
#include <string>
#include <windows.h>
using namespace std;
int main() {
    int n;
    long long s;
    long long a = 1;
    long long b = 1;

    while (1)
    {
        system("cls");
        cout << "请输入斐波那契数列的个数:";
        cin >> n;
        if (n > 0){
            break;       
        }
        else {
            cout << "请输入大于0的整数!" << endl;          
        }
    }
    for (int i = 1; i < 3; i++) {
        cout << "1 ";
    }
    for (int i = 3; i <= n; i++) {
        s = a + b;
        a = b;
        b = s;
        cout << s << " ";
    }
    cout << endl;
    system("pause");
    return 0;
}


本题主要考察if语句和for循环的应用
posted on 2022-08-08 18:52  wshidaboss  阅读(80)  评论(0编辑  收藏  举报