2.
#define _USE_MATH_DEFINES #include#include #include using namespace std; int main() { float r, h, l, S, S1, V, V1; cin >> r >> h; l = 2 * M_PI * r; S = M_PI * r * r; S1 = 4 * M_PI * r * r; V = 4 / 3 * M_PI * r * r * r; V1 = S * h; cout << setiosflags(ios::fixed) << setprecision(2); cout << "圆周长 l = " << l << '\t' << "圆面积 S = " << S << endl; cout << "圆球表面积 S1 = " << S1 << '\t' << "圆球体积 V = " << V << endl; cout << "圆柱体积 V1 = " << V1 << endl; return 0; }
结果:
3.
#include#include using namespace std; int main() { float F, c; cout << "华氏温度 F = "; cin >> F; cout << setiosflags(ios::fixed) << setprecision(2); c = (5.0 / 9.0) * (F - 32); cout << "摄氏温度c = " << c << endl; return 0; }
结果:
4.
c1,c2定义为字符型
#includeusing namespace std; int main() { char c1, c2; cout << "请输入两个字符"; c1 = getchar(); c2 = getchar(); cout << "putchar 输出结果"; putchar(c1); putchar('\t'); putchar(c2); putchar('\n'); cout << "cout输出结果"; cout << c1 << '\t' << c2 << endl; return 0; }
结果:
c1,c2定义为整形
#includeusing namespace std; int main() { int c1, c2; cout << "请输入两个字符"; c1 = getchar(); c2 = getchar(); cout << "putchar 输出结果"; putchar(c1); putchar('\t'); putchar(c2); putchar('\n'); cout << "cout输出结果"; cout << c1 << '\t' << c2 << endl; return 0; }
结果:
定义为整形即可输出ASCII码。
8.
0
1
1
0
1
9.
#includeusing namespace std; int main() { int compare(int x, int y, int z); int a, b, c, temp, max; cout << "输入三个整数a b c" << endl; cin >> a >> b >> c; //第一种方法 /*if ((a > b) && (a > c)) cout << "最大的数为a = " << a << endl; else if((b>a)&&(b>c)) cout << "最大的数为b = " << b << endl; else cout << "最大的数为c = " << c << endl;*/ //第二种方法 /*if(a*/ //第三种方法 /*temp = (a > b) ? a : b; max = (temp > c) ? temp : c; cout << "最大的数为 " << max << endl;*/ max = compare(a, b, c); cout << "最大的数为 " << max << endl; return 0; } int compare(int x, int y, int z) { int temp, max; temp = (x > y) ? x : y; max = (temp > z) ? temp : z; return max; }
结果:
10.
#includeusing namespace std; int main() { int x, y; cout << "输入 x = "; cin >> x; if (x < 1) y = x; else if (x >= 1 && x < 10) y = 2 * x - 1; else y = 3 * x - 11; cout << "输出 y = " << y << endl; return 0;
结果:
11.
#includeusing namespace std; int main() { int grade; while (1) { cout << "输入学生成绩grade = "; cin >> grade; if (grade < 0 || grade > 100) cout << "成绩输入错误,请重新输入!" << endl; else switch ((int)grade / 10) { case 10: cout << grade << "分成绩等级为A" << endl; break; case 9: cout << grade << "分成绩等级为A" << endl; break; case 8: cout << grade << "分成绩等级为B" << endl; break; case 7: cout << grade << "分成绩等级为C" << endl; break; case 6: cout << grade << "分成绩等级为D" << endl; break; default: cout << grade << "分成绩等级为E" << endl; break; } } return 0; }
结果:
12.
#includeusing namespace std; int main() { long int a; cout << "输入正整数 a = "; while (1) { cin >> a; if (a > 99999) { cout << "输入错误,请重新输入!" << endl; continue; } else if (a / 10000 > 0) cout << "a 为5位数 " << a / 10000 + (a % 10000) / 1000 * 10 + (a % 1000) / 100 * 100 + (a % 100) / 10 * 1000 + (a % 10) * 10000 << endl; else if (a / 1000 > 0) cout << "a 为4位数 " << a / 1000 + (a % 1000) / 100 * 10 + (a % 100) / 10 * 100 + (a % 10) * 1000 << endl; else if (a / 100 > 0) cout << "a 为3位数 " << a / 100 + (a % 100) / 10 * 10 + (a % 10) * 100 << endl; else if (a / 10 > 0) cout << "a 为2位数 " << a / 10 + (a % 10) * 10 << endl; else cout << "a 为1位数 " << a << endl; } return 0; }
结果:
13.
#includeusing namespace std; int main() { float i; int c; while (1) { /*cout << "输入当月利润为 i = "; cin >> i; if (i <= 10) cout << "应发奖金为 " << i*0.1 << "万元" << endl; else if (i <= 20 && i >= 10) cout << "应发奖金为 " << 10 * 0.1 + (i - 10)*0.075 << "万元" << endl; else if (i <= 40 && i >= 20) cout << "应发奖金为 " << 10 * 0.1 + 10 * 0.075 + (i - 20)*0.05 << "万元" << endl; else if (i <= 60 && i >= 40) cout << "应发奖金为 " << 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + (i - 40)*0.03 << "万元" << endl; else if (i <= 100 && i >= 60) cout << "应发奖金为 " << 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + 20 * 0.03 + (i - 60)*0.015 << "万元" << endl; else cout << "应发奖金为 " << 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + 20 * 0.03 + 40 * 0.015 + (i - 100)*0.01 << "万元" << endl;*/ cout << "输入当月利润为 i = "; cin >> i; c = i / 10; if (c > 10) c = 10; switch (c) { case 0: cout << "应发奖金为 " << i*0.1 << "万元" << endl; break; case 1: cout << "应发奖金为 " << 10 * 0.1 + (i - 10)*0.075 << "万元" << endl; break; case 2: case 3: cout << "应发奖金为 " << 10 * 0.1 + 10 * 0.075 + (i - 20)*0.05 << "万元" << endl; break; case 4: case 5: cout << "应发奖金为 " << 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + (i - 40)*0.03 << "万元" << endl; break; case 6: case 7: case 8: case 9: cout << "应发奖金为 " << 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + 20 * 0.03 + (i - 60)*0.015 << "万元" << endl; break; case 10: cout << "应发奖金为 " << 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + 20 * 0.03 + 40 * 0.015 + (i - 100)*0.01 << "万元" << endl; break; } } return 0;
结果:
14.
#includeusing namespace std; int main() { void sort(int m, int j, int k, int l); int a, b, c, d; cout << "请输入四个整数 "; cin >> a >> b >> c >> d; sort(a, b, c, d); return 0; } void sort(int m, int j, int k, int l) { int temp; if (m > j) { temp = m; m = j; j = temp; } if (m > k) { temp = m; m = k; k = temp; } if (m > l) { temp = m; m = l; l = temp; } if (j > k) { temp = j; j = k; k = temp; } if (j > l) { temp = j; j = l; l = temp; } if (k > l) { temp = k; k = l; l = temp; } cout << "从小到大顺序输出为:" << m << '\t' << j << '\t' << k << '\t' << l << '\t' << endl; }
结果:
15.
#includeusing namespace std; int main() { int m, n, temp, p; while (1) { temp = 1; cout << "请输入两个正整数:"; cin >> m >> n; p = m * n; while (temp != 0) { if (m > n) { temp = m % n; m = n; n = temp; if (temp == 0) { cout << "m 和 n 的最大公约数为:" << m << endl; cout << "m 和 n 的最小公倍数为:" << p / m << endl; } } else { temp = n % m; n = m; m = temp; if (temp == 0) { cout << "m 和 n 的最大公约数为:" << n << endl; cout << "m 和 n 的最小公倍数为:" << p / n << endl; } } } } return 0; }
结果:
16.
#includeusing namespace std; int main() { char c; int l = 0, s = 0, d = 0, o = 0; cout << "请输入一行字符" << endl; while (1) { while ((c = getchar()) != '\n') { if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z') l++; else if (c == ' ') s++; else if (c >= '0' && c <= '9') d++; else o++; } cout << "这一行字符中" << "英文字母" << l << "个" << endl; cout << "这一行字符中" << "空格" << s << "个" << endl; cout << "这一行字符中" << "数字" << d << "个" << endl; cout << "这一行字符中" << "其他字符" << o << "个" << endl; l = 0, s = 0, d = 0, o = 0; }return 0; }
结果:
17.
#includeusing namespace std; int main() { int a, Sn = 0, n, i = 1, temp = 0; cout << "输入 a 的值: "; cin >> a; cout << "输入 n 的值: "; cin >> n; while (i <= n) { temp = temp + a; Sn = Sn + temp; a = a * 10; i++; } cout << "Sn = " << Sn << endl; return 0; }
结果:
18.
#includeusing namespace std; int main() { int n = 1, S = 0, temp = 1; while (n <= 20) { temp = temp*n; S = S + temp; n++; } cout << S << endl; return 0; }
结果:
19.
#includeusing namespace std; int main() { int a, b, i, j, k; for (a = 100; a < 1000; a++) { i = a / 100; j = (a % 100) / 10; k = a % 10; b = i*i*i + j*j*j + k*k*k; if (a == b) cout << a << " 是水仙花数" << endl; } return 0; }
结果:
20.
#includeusing namespace std; int main() { int m, n, i; for (m = 2; m<1000; m++) { n = 0; for (i = 1; i ) if ((m%i) == 0) n = n + i; if (n == m) { cout << m << ",its factors are:" ; for (i = 1; i ) if (m%i == 0) cout << i << " "; cout << endl; } } return 0; }
结果:
21.
#includeusing namespace std; int main() { int i, t, n = 20; double a = 2, b = 1, s = 0; for (i = 1; i <= n; i++) { s = s + a / b; t = a; a = a + b; // 将前一项分子与分母之和作为下一项的分子 b = t; // 将前一项的分子作为下一项的分母 } cout << "sum=" << s << endl; return 0; }
结果:
22.
#includeusing namespace std; int main() { int n = 1, i; for (i = 1; i < 10; i++) { n = (n + 1) * 2; } cout << "桃子总数" << n << endl; return 0; }
结果:
23.
#include#include using namespace std; int main() { float a, x0, x1; cout << "enter a positive number: "; cin >> a; x0 = sqrt(double(a)); x1 = (x0 + a / x0) / 2; do { x0 = x1; x1 = (x0 + a / x0) / 2; } while (fabs(x0 - x1) >= 1e-5); cout << "The square root of " << a << " is " << x1 << endl; return 0; }
结果:
24.
#includeusing namespace std; int main() { int i, k; for (i = 0; i <= 3; i++) // 输出上面4行*号 { for (k = 0; k <= 2 * i; k++) cout << "*"; // 输出*号 cout << endl; //输出完一行*号后换行 } for (i = 0; i <= 2; i++) // 输出下面3行*号 { for (k = 0; k <= 4 - 2 * i; k++) cout << "*"; // 输出*号 cout << endl; // 输出完一行*号后换行 } return 0; }
结果:
25.
#includeusing namespace std; int main() { char i, j, k; /* i是a的对手;j是b的对手;k是c的对手*/ for (i = 'X'; i <= 'Z'; i++) for (j = 'X'; j <= 'Z'; j++) if (i != j) for (k = 'X'; k <= 'Z'; k++) if (i != k && j != k) if (i != 'X' && k != 'X' && k != 'Z') cout << "A--" << i << " B--" << j << " C--" << k << endl; return 0; }
结果:
参考链接:http://blog.csdn.net/zhengxiaoyang995926/article/details/79491582