上机作业
1、输入三个实数,按从大到小的顺序输出。
#include <iostream>
using namespace std;
int main()
{
float a, b, c;
cin >> a >> b >> c;
if (a > b)
{
if (b > c)
{
cout << a<<" " << b << " " << c;
}
else if (b<c)
{
cout << a << " " << c << " " << b;
}
else
{
cout << c << " " << a << " " << b;
}
}
else
{
if (b > c)
{
if (a > c)
{
cout << b << " " << a << " " << c;
}
else
{
cout << b << " " << c << " " << a;
}
}
else
{
cout << c << " " << b << " " << a;
}
}
return 0;
}
2、采用海伦公式,求三角形的面积,三角形的三个边从键盘输入。
#include<cmath>
using namespace std;
int main()
{
float a, b, c, S,p;
cout << "请输入三角形三边" << endl;
cin >> a >> b >> c;
p = (a + b + c) / 2;
S = sqrt(p * (p - a) * (p - b) * (p - c));
cout << "三角形的面积是" << S << endl;
return 0;
}
3、打印出所有的“水仙花数”,所谓“水仙花数”是指一个三位
数,其各位数字的立方和等于该数本身。如:153 是一水仙花数,
因为 153=13+53+33
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int num, a, b, c;
for (num = 100; num < 1000; num++)
{
a = num / 100;
b = num / 10 % 10;
c = num % 10;
if (a* a *a+ b * b *b+ c * c*c ==num)
{
cout << num << endl;
}
}return 0;
}
4、从键盘输入一个数 N,计算 1+2+3+……+N 的值;要求能一直输
入一直计算,直到输入一个负数停止。
#include <iostream>
using namespace std;
int main()
{
int N, sum = 0;
while (1)
{
cin >> "请输入一个数" >> N >> endl;
while (N > 0)
{
for (int i = 1; i <= N; i++)
{
sum = sum + i;
}
}
cout << "结果是" << sum << endl;
}
return 0;
}
5、输出以下 4*5 的矩阵。
#include<iostream>
using namespace std;
int main()
{
int a, b, c, d, e, n;
a = 1, b = 2, c = 3, d = 4, e = 5;
n = 1;
while (n < 5)
{
n =n+1 ;
cout << a <<'\t'<< b << '\t' << c << '\t' << d << '\t' << e << endl;
a = a + 1, b = b + 2, c = c + 3, d = d + 4, e = e + 5;
}
return 0;
}
6、从键盘随机输入 10 个数,统计其中正数的个数及其平均数
#include<iostream>
using namespace std;
int main()
{
float a, b = 0, n,average,t=0;
for (n = 1; n < 11; n = n + 1)
{
cin >> a;
if (a > 0)
{
b = b + 1;
t=t+a;
}
average = t / b;
}
cout <<"正数的个数是"<< b <<"平均数是" <<average << endl;
return 0;
}
7、已知半径 radius,采用循环结构计算半径 1~10 区间内圆的面积
area。且当 area>100 时结束程序运行。
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
double radius=1, S,pi=3.14;
while (radius < 11)
{
S = pi * radius * radius;
radius = radius + 1;
if (S > 100)
{
cout << endl; break;
}
else
{
cout << "面积是" << S << endl;
}
}return 0;
}
8、从键盘输入一个数 m,判断这个数是否为素数
#include<iostream>
#include <cmath>
using namespace std;
int main()
{
int m,x,i;
cout << "请输入一个数";
cin >> m;
x = sqrt(m);
if (m != 1)
{
for (i = 2; i <= x; i++)
{
if (m% i ==0)
{
cout << "合数" << endl; break;
}
else
{
cout << "素数" << endl; break;
}
}
}
else
{
cout << "及不是素数也不是合数" << endl;
}
return 0;
}
9、求 100-200 之间的全部素数并按照每 5 个一行进行输出
#include <iostream>
#include<cmath>
using namespace std;
int main()
{
int i, j, k,n;
k = 0;
for (i = 100; i <201; i += 1)
{
n = sqrt(i);
for (j = 2; j <= n; j++)
{
if (i % j == 0)
break;
}
if (j==n+1)
{
cout << i << '\t';
k++;
if (k % 5 == 0)
cout << endl;
}
}
return 0;
}
10、从键盘输入一个正整数 n,输出一个星型图案,其中第 n 行的*
号个数为 2n-1 个。
#include<iostream>
using namespace std;
int main()
{
int a, c, n, b, i, N;
cout << "请输入一个数";
cin >> n;
b = n;
for (a = 1, c = 0; a < n && b >= 0; ++a, --b)
{
for (i = 1; i < b; ++i)
{
cout << " ";
}
c++;
for (i = 1; i < 2 * c; i++)
{
cout << "*";
}
cout << endl;
}
for (N = 1; N < 2 * n; ++N)
{
cout << "*";
}
cout << endl;
for (a = 1, c = 2 * n - 1, b = n; a < n && b >= 0; ++a, --b)
{
for (i = 1; i <= a; ++i)
{
cout << " ";
}
c = c - 2;
for (i = 1; i <= c; ++i)
{
cout << "*";
}
cout << endl;
}
return 0;
}