博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++程序设计(第二版)谭浩强----程序题课后习题答案第三章
阅读量:5874 次
发布时间:2019-06-19

本文共 10459 字,大约阅读时间需要 34 分钟。

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定义为字符型

#include 
using 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定义为整形

#include 
using 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.

#include 
using 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
y) ? x : y; max = (temp > z) ? temp : z; return max;}

 

结果:

 

10.

#include 
using 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.

#include 
using 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.

#include 
using 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.

 

#include
using 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.

#include
using 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.

#include
using 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.

#include 
using 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.

 

#include
using 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.

#include
using 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.

#include
using 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.

#include 
using namespace std;int main(){ int m, n, i; for (m = 2; m<1000; m++) { n = 0; for (i = 1; i

结果:

 

21.

#include 
using 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.

#include 
using 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.

#include 
using 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.

#include 
using 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;}

结果:

 

参考链接:

转载于:https://www.cnblogs.com/MrZheng9511/p/c3.html

你可能感兴趣的文章
vmware虚拟机里的LINUX不能上网的原因一:虚拟网卡设置
查看>>
监控摄像机的区别和分类
查看>>
Java学习——对象和类
查看>>
ElasticSearch 组合过滤器
查看>>
HttpClient连接池的连接保持、超时和失效机制
查看>>
1-4 多文档界面处理(2)
查看>>
《Essential Linux Device Drivers》中文版第1章
查看>>
让远程传输大文件变得更快
查看>>
complex的小困惑
查看>>
十进制、十六进制、二进制的转换
查看>>
双网卡centos7 iptables防火墙与/etc/rc.d/rc.local开机运行
查看>>
tomcat PermGen space 不足的解决方法
查看>>
STM32系统滴答_及不可不知的延时技巧 - (上)
查看>>
Linux下企业级分区方案
查看>>
CentOS下LAMP一键yum安装脚本
查看>>
拖来拖去今天终于重装系统了
查看>>
NestJS 脑图
查看>>
我的友情链接
查看>>
Html body的滚动条禁止与启用
查看>>
Tengine新增nginx upstream模块的使用
查看>>