第一次上机作业
1、输入三个实数,按从大到小的顺序输出。
#include<iostream>
using namespace std;
int main()
{
int n1, n2, n3;
int max, min;
cout << "请输入n1,n2,n3:";
cin >> n1 >> n2 >> n3;
if (n1 > n2) {
max=n1;
min=n2;
}
else {
max = n2;
min = n1;
}
if (n3 > max) {
cout << n3 << max << min << endl;
}
else if (n3 < min) {
cout << max << min << n3 << endl;
}
else {
cout << max << n3 << min << endl;
}
return 0;
}
2、采用海伦公式,求三角形的面积,三角形的三个边从键盘输入。
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double a, b, c;
double p, s;
cout<<"请输入a,b,c:";
cin >> a >> b >> c;
p = (a + b + c) / 2;
s = sqrt(p * (p - a) * (p - b) * (p - c));
cout << "s=" << s << endl;
return 0;
}
3、打印出所有的“水仙花数”,所谓“水仙花数”是指一个三位
数,其各位数字的立方和等于该数本身。
#include <iostream>
using namespace std;
int main()
{
int a=1, b=0, c=0;
int d, e, f;
int sum;
for (a = 1; a <= 9; a++)
{
for (b = 0; b <= 9; b++)
{
for (c = 0; c <= 9; c++)
{
d = a * 100;
e = b * 10;
f = c * 1;
sum = d + e + f;
int s1;
s1 = a * a * a + b * b * b + c * c * c;
if (sum == s1)
{
cout << s1 << "为水仙花数" << endl;
}
}
}
}
return 0;
}
4、从键盘输入一个数 N,计算 1+2+3+……+N 的值;要求能一直输
入一直计算,直到输入一个负数停止
#include <iostream>
using namespace std;
int main()
{
do{
int a;
a = 0;
int N;
int sum;
sum = 0;
cout << "请输入N:";
cin >> N;
if (N >= 0)
{
do {
sum += a++;
} while (a <= N);
cout << sum;
}
else {
break;
}
} while (1);
}
5、输出以下 4*5 的矩阵。
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
#include <iostream>
using namespace std;
int main()
{
int a, b, c;
for (a = 1; a <= 4; a++)
{
for (b = 1; b <= 5; b++)
{
c = a * b;
cout << c << "\t";
}
cout << endl;
}
return 0;
}
6、从键盘随机输入 10 个数,统计其中正数的个数及其平均数
#include <iostream>
using namespace std;
int main()
{
int a, b, c, d, e, f, g, h, i, j;
int n = 0;
cout << "请输入数字:";
cin >> a;
do {
if (a > 0)
{
n = n++;
}
else {
break;
}
cout << n << endl;
} while (0);
cout << "请输入数字:";
cin >> b;
do {
if (b > 0)
{
n = n++;
}
else {
break;
}
cout << n << endl;
} while (0);
cout << "请输入数字:";
cin >> c;
do {
if (c > 0)
{
n = n++;
}
else {
break;
}
cout << n << endl;
} while (0);
cout << "请输入数字:";
cin >> d;
do {
if (d > 0)
{
n = n++;
}
else {
break;
}
cout << n << endl;
} while (0);
cout << "请输入数字:";
cin >> e;
do {
if (e > 0)
{
n = n++;
}
else {
break;
}
cout << n << endl;
} while (0);
cout << "请输入数字:";
cin >> f;
do {
if (f > 0)
{
n = n++;
}
else {
break;
}
cout << n << endl;
} while (0);
cout << "请输入数字:";
cin >> g;
do {
if (g > 0)
{
n = n++;
}
else {
break;
}
cout << n << endl;
} while (0);
cout << "请输入数字:";
cin >> h;
do {
if (h > 0)
{
n = n++;
}
else {
break;
}
cout << n << endl;
} while (0);
cout << "请输入数字:";
cin >> i;
do {
if (i > 0)
{
n = n++;
}
else {
break;
}
cout << n << endl;
} while (0);
cout << "请输入数字:";
cin >> j;
do {
if (j > 0)
{
n = n++;
}
else {
break;
}
cout << n << endl;
} while (0);
int X;
X = (a + b + c + d + e + f + g + h + i + j) / 10;
cout << X << endl;
return 0;
}
7、已知半径 radius,采用循环结构计算半径 1~10 区间内圆的面积
area。且当 area>100 时结束程序运行
#include <iostream>
using namespace std;
int main()
{
double p;
p = 3;
double r;
for (r = 1; r <= 10; r++)
{
int s;
s = p * r * r;
cout << s << endl;
if (s > 100)
{
cout << s << endl;
break;
}
}
return 0;
}
8、从键盘输入一个数 m,判断这个数是否为素数。
#include <iostream>
using namespace std;
int main()
{
int m;
cout << "请输入数m:";
cin >> m;
int a;
for (a = 2; a < m; a++)
{
if (m% a == 0) {
cout << "该数不是素数:";
}
else {
cout << "该数是素数:";
}
return 0;
}
}
9、求 100-200 之间的全部素数并按照每 5 个一行进行输出
#include <iostream>
using namespace std;
int main()
{
int a = 0;
int n,b;
for (n = 100; n <= 200; n++)
{
for (b = 2; b < n; b++)
{
if (n % b == 0)
break;
else
continue;
}
if (n == b){
cout << n << " ";
a++;
}
if (a%5== 0)
cout << endl;
}
return 0;
}
10、从键盘输入一个正整数 n,输出一个星型图案,其中第 n 行的*
号个数为 2n-1 个。
#include <iostream>
using namespace std;
int main()
{
int n;
int a,b;
cout << "请输入数字n:";
cin >> n;
//上半层
for (a = 0; a < n; a++)//外层控制循环次数,输入的数字就是循环的次数
{ //n-1比外层循环少一次,减去i就是每行少一个空格
for (b = 0; b < (n - 1) - a; b++)//内层循环打印空格的个数,外层循环n次,内层循环n-1次
{//循环几次几个空格
cout << " ";//第n层的时候a=-1空格为0所以这层不用循环 所以循环次数变为n-1
}//星星的个数是1 3 5 7...2*n-1的等差数列形式递增的
for (b = 0; b < 2 * a + 1; b++)//内层循环打印一行上星星的个数a=0时b<1循环一次打印一颗星星
//循环几次几颗星星a=1时b<3(<=2)循环两次,以此类推。原因是b是从0开始的
{
cout << "*";
}
cout << endl;
}
//下半层
for (a = 0; a < n - 1; a++)//外层控制循环次数,输入的数字就是循环的次数
//由于下半层是共有n-1层故外层只需要循环n-1次 所以a<n-1
{
for (b = 0; b < a + 1; b++)//下半层自上到下空格数增多循环次数也跟着增多
{
cout << " ";
}
for (b = 0; b < 2 * (n - 1) - (1 + 2 * a); b++)//下半层自上到下星星数依次递减,循环次数也跟着减少
//由于b=0故外层循环次数为 2 * (n - 1) - (1 + 2 * a)
{
cout << "*";//星星的个数是2*n-1 2*n-3 2*n-5...2*n-(2*a+1)等差数列递减
//原因如上n=3,第一次a=0, 2 * (n - 1) - (1 + 2 * a)=3故下层的第一层循环了三次打出三个*
}
cout << endl;
}
return 0;
}
//第十题查了资料,注释为查后的自己理解