#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循环的应用