1.输入三个实数,按从小到大的顺序输出。
#include<iostream>
using namespace std;
int main()
{
int a, b, c;
cout << "请输入a,b,c" << endl;
cin >> a >> b >> c;
if (a > b)
{
if (b > c)
{
cout << "a>b>c" << endl;
}
else {
if (c > a)
{
cout << "c>a>b" << endl;
}
else
{
cout << "a>c>b" << endl;
}
}
}
else
{
if (a > c)
{
cout << "b>a>c" << endl;
}
else {
if (b > c)
{
cout << "b>c>a" << endl;
}
else
{
cout << "c>b>a" << endl;
}
}
}
return 0;
}
2.采用海伦公式,求三角形的面积。
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
double a, b, c,p;
cout << "请输入三角形的三边a,b,c" << endl;
cin >> a >> b >> c;
p = (a + b + c) / 2;
cout << sqrt(p * (p - a) * (p - b) * (p - c))<< endl;
return 0;
}
3水仙花数。
include<iostream>
using namespace std;
int main()
{
for (int i = 1;i <= 9;i++){
for (int j = 0;j <= 9;j++) {
for (int h=0;h <= 9;h++) {
if (i * 100 + j * 10 + h == i * i * i + j * j * j + h * h * h) {
cout << i * 100 + j * 10 + h << endl;
}
}
}
}
return 0;
}
4求和。
#include<iostream>
using namespace std;
int main()
{
int N,num=0;
cin >> N;
while (N >= 0)
{
num = N * (N - 1) / 2;
cout << num << endl;
cin >> N;
}
return 0;
}
5输入以下4*5矩阵。
#include<iostream>
using namespace std;
int main()
{
int i = 1, b = 1;
while (i <= 4)
{
b = 1;
while (b <= 5)
{
cout << i * b << " ";
b++;
}
i++;
cout << endl;
}
return 0;
}
6求正数个数,及平均数。
#include<iostream>
using namespace std;
int main()
{
int a,num=0,j=0,h;
cout << "请输入任意10个数" << endl;
for (int i = 0;i < 10;i++)
{
cin >> a;
if (a > 0)j++;
{
num += a;
}
}
h=num/j;
cout << j << endl;
cout<< h << endl;
return 0;
}
7求面积。
#include<iostream>
using namespace std;
int main()
{
int pi = 3.14,area=0;
for (double radius = 1;radius <= 10;radius++) {
area = pi * radius * radius;
if (area > 100)break;
}
return 0;
}
8判断素数
#include<iostream>
using namespace std;
int main()
{
int m;
int b = 1;
cin >> m;
if (m == 2) {
cout << "yes" << endl;
}
else if (m == 1)
{
b = 0;
cout << endl;
}
else if (m>2){
for (int i = 2;i < m;i++)
{
if (m % i != 0) continue;
else {
b = 0;
cout << "no" << endl;
}break;
}
}
if (b)
{
cout << "yes" << endl;
}
return 0;
}
9素数2.0
#include<iostream>
using namespace std;
int main()
{
int m = 1,h=1;
int i = 100;
for (;i <= 200;i++)
{
m = 1;
for (int j = 1;j < i;j++)
if (j == 1)
{
continue;
}
else if (i % j != 0)continue;
else {
m = 0;
break;
}
if (m)
{
if (h == 5) {
cout << i << endl;
h = 1;
}
else {
cout << i<< " ";
h++;
}
}
}
return 0;
}
10星星星
#include<iostream>
using namespace std;
int main()
{
cout << "请输入一个数n" << endl;
int n;
cin >> n;
for (int i = 1;i <= n;i++)
{
for (int j=1;j<=n-i;j++)
{
cout << " ";
}
for (int m = 1;m <= 2 * i-1;m++)
{
cout << "*";
}
cout << endl;
}
for (int a = 1;a <= n;a++)
{
for (int b = 1;b <= a;b++)
{
cout << " ";
}
for (int c = 1;c <= 2 * n - 1 - 2 * a;c++) {
cout << "*";
}
cout << endl;
}
return 0;
}