项目1:输出星号
#include <iostream>
using namespace std;
int main() {
for (int j = 5; j > 0; j--)
{
int i = j;
while (i--) {
cout << ' ';
}
cout << '*';
if(j!=5) {
int k = 1 + (4 - j) * 2;
while (k--) {
cout << ' ';
}
cout << '*';
}
cout << endl;
}
int i = 11;
while (i--)
cout << '*';
cout << endl;
return 0;
}
项目2:输出质数
[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
#include <iostream>
using namespace std;
bool prime(int n);
int main() {
for (int i = 1000; i < 10000; i++) {
if(prime(i) && (i % 10 == i / 1000))
cout << i << ' ';
}
cout << endl;
return 0;
}
bool prime(int n) {
for (int i = 2; i < n; i++) {
if (n % i == 0)
return 0;
}
return 1;
}
项目3:输出完数
[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
#include <iostream>
using namespace std;
int com(int n);
int main() {
for (int i = 1; i < 1000; i++) {
if (i == com(i))
cout << i << ' ';
}
cout << endl;
return 0;
}
int com(int n) {
int s = 0;
for (int i = 1; i <= n / 2; i++) {
if(n % i == 0)
s += i;
}
return s;
}
项目4:贪财的富翁
[cpp] view plain copy
#include <iostream>
using namespace std;
int main() {
double s1, s2;
s1 = s2 = 0;
s1 = 30 * 100000;
for (double i = 0.01,k = 1; k < 31; i *= 2, k++) {
s2 += i;
}
cout.setf(ios::scientific);
cout << "s1 " << s1 << endl << "s2 " << s2 << endl;
return 0;
}
项目5:百钱百鸡问题
[cpp] view plain copy
#include <iostream>
using namespace std;
int main() {
int a, b, c;
for (a = 0; a < 21; a++)
for (b = 0; b < 34; b++) {
c = 100 - a - b;
if ((c % 3 == 0) && (5 * a + 3 * b + c / 3 == 100))
cout << "鸡翁" << a << "只, "
<< "鸡母" << b << "只, "
<< "鸡雏" << c << "只。" << endl;
}
return 0;
}