Compare commits

...

3 Commits

Author SHA1 Message Date
flykhan 7b07eb048f 斐波那契数列 2023-06-24 10:38:43 +08:00
flykhan eae410f777 输出一个数的阶乘 2023-06-24 10:23:45 +08:00
flykhan 5434ce3192 输出一个数的所有因数 2023-06-24 10:19:24 +08:00
3 changed files with 71 additions and 0 deletions

22
day7/homework/oh1.cpp Normal file
View File

@ -0,0 +1,22 @@
// 输入一个整数,输出它的所有因数。
// 例如:输入 12输出 1 2 3 4 6 12
#include <iostream>
using namespace std;
int main()
{
int n;
cout << "请输入一个整数:";
cin >> n;
int i = 1;
while (i <= n)
{
if (n % i == 0)
cout << i << "\t";
i++;
}
return 0;
}

22
day7/homework/oh2.cpp Normal file
View File

@ -0,0 +1,22 @@
// 输入一个整数,输出它的阶乘。
// 例如输入5输出120
#include <iostream>
using namespace std;
int main()
{
int n;
int sum = 1;
cout << "请输入一个整数:";
cin >> n;
while (n)
{
sum *= n;
--n;
}
cout << sum << endl;
return 0;
}

27
day7/homework/oh3.cpp Normal file
View File

@ -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;
}