1
#include<iostream>
using namespace std;
int main() {
double a, b, c, t;
cout << "请输入三个正数" << endl;
cin >> a >> b >> c;
if (b >= a) {
t = b;
b = a;
a = t;
}
if (c >= a)
{
a = c;
c = b;
b = t;
}
else if (c>=b) {
t = c;
c = b;
b = t;
}
cout << a << " " << b << " " << c << endl;
}
2
本题我并未考虑输入的三个数是否为三角形,因为我觉得应该是有一个三角形之后才会去求它的面积,是否为三角形不在我的解题内容中;
#include<iostream>
#include<cmath>
using namespace std;
int main() {
double a, b, c, p, s;
cout << "请依次输入三边长度" << endl;
cin >> a >> b >> c;
p = (a + b + c) / 2;
s = sqrt(p * (p - a) * (p - b) * (p - c));
cout << "三角形的面积是" << s << endl;
}
3
本题中,我将a,b,c设为整型,这样在整数x除以100,10时,便可舍弃小数点后的值,取到其百位与十位,从而计算出其各位,在判断是否为水仙花数。
#include<iostream>
using namespace std;
int main() {
for (int x = 100; x < 1000; x++)
{
int a, b, c;
a = x / 100;
b = x / 10 - 10 * a;
c = x - 100 * a - 10 * b;
if (x == a * a * a + b * b * b + c * c * c)
cout << x << endl;
}
return 0;
}
4
#include<iostream>
using namespace std;
int main() {
int N, s;
while (1) {
cin >> N;
if (N < 0)break;
s = N * (N + 1) / 2;
}
return 0;
}
5
按照数学规律23*4得出矩阵
#include<iostream>
using namespace std;
int main() {
for (int n=1; n < 5; n++){
cout << n << " " << n * 2 <<" "<< n * 3 << " " << n * 4 << " "<<n*5<< endl;
}
return 0;
}
6
#include<iostream>
using namespace std;
int main() {
double s = 0,n;
int t = 0;
for (int m = 1; m < 11;m++) {
cout << "兄弟,输个数" << endl;
cin >> n;
if (n > 0) {
;
t++;
s += n;
}
if (m < 10)continue;
cout << "正数的个数是" << t << endl;
cout << "其平均数为" << s / t;
}
return 0;
}
7
#include<iostream>
using namespace std;
int main() {
double radius, area,pi=3.14;
while (1) {
cout << "请输入半径在1到10的数";
cin >> radius;
if (radius > 10 || radius < 1)continue;
area = pi * radius * radius;
if (area > 100)break;
cout << area;
}
}
8
n=2时单独考虑
#include<iostream>
using namespace std;
int main() {
int m,n,t;
cout << "请输入一个数" << endl;
cin >> m;
n = m - 1;
for (; n >2; n--) {
if (m % n == 0)break;
}
while (1) {
if (n == 2)
cout << "该数为素数" << endl; break;
t = m % n;
if (!(t == 0))
cout << "该数为素数" << endl;
if (t == 0)
cout << "该数不为素数" << endl;
}
return 0;
}
9
#include<iostream>
#include<cmath>
using namespace std;
int main() {
int t = 0;
for (int a = 100; a < 201; a++) {
int i = 2;
for (; i < sqrt(a); i++)
if (a % i == 0)break;
if (!(a % i == 0)) {
t++;
cout << a<<" ";
if(t % 5 == 0)
cout << endl;
}
}
}
10
每一行有2n-1个,n-1个空格
#include<iostream>
using namespace std;
int main() {
int n;
cout << "请输入一个正整数"<<endl;
cin >> n;
for (int a = 1; a < n; a++) {
for (int f = 1; f <= n - a; f++)
cout << " ";
for (int b = 1; b <= 2*a-1; b++) {
cout << "*";
}
cout << endl;
}
for (int c = 1; c <= 2*n-1; c++)
cout << "*" ;
cout << endl;
for (int e = n-1; e >0; e--) {
for (int g = 1; g <= n - e;g++)
cout << " ";
for (int d =1; d <=2*e-1; d++)
cout << "*";
cout << endl;
}
}