qfedu-basic-level/day7/homework/oh3.cpp

32 lines
510 B
C++
Raw Normal View History

2023-06-24 10:38:43 +08:00
// 输入一个整数,输出它的斐波那契数列。
// 例如输入10输出1 1 2 3 5 8
#include <iostream>
using namespace std;
int main()
{
int n;
cout << "请输入一个整数:";
cin >> n;
int a = 1;
int b = 1;
int c = 0;
2023-06-24 10:43:50 +08:00
if (n >= 1)
cout << a << "\t";
if (n >= 2)
cout << b << "\t";
while (n > 2 && c <= n)
2023-06-24 10:38:43 +08:00
{
c = a + b;
if (c <= n)
cout << c << "\t";
a = b;
b = c;
}
return 0;
}