编程语言
首页 > 编程语言> > 用C++输出指定项的斐波那契数列

用C++输出指定项的斐波那契数列

作者:互联网

#include <iostream>
#include <Windows.h>
#include <string>
using namespace std;
int main() {
    int n;
    long long s;
    long long a = 1;
    long long b = 1;
    cout << "请输入斐波那契数列的个数:";
    cin >> n;
    if (n <= 0) {
        cout << "请输入大于0的整数:";
    }
    else
    {
        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循环的应用。

 

标签:std,main,int,namespace,long,斐波,C++,那契,include
来源: https://www.cnblogs.com/smartlearn/p/16563079.html