#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循环的应用。
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/279618.html