斐波那契数列
This commit is contained in:
parent
eae410f777
commit
7b07eb048f
|
@ -0,0 +1,27 @@
|
|||
// 输入一个整数,输出它的斐波那契数列。
|
||||
// 例如:输入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;
|
||||
cout << a << "\t" << b << "\t";
|
||||
while (c <= n)
|
||||
{
|
||||
c = a + b;
|
||||
if (c <= n)
|
||||
cout << c << "\t";
|
||||
a = b;
|
||||
b = c;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue