2021/10/20
1.设圆半径r=1.5,圆柱高h=3,求圆周长、圆面积、圆球体表面积、圆球体积、圆柱体积。用cin输入数据,输出计算结果。
#include<iostream>
#include<iomanip>
using namespace std;
int main() {
double r, h;
double c, s1, s2, v1, v2;
cout << "please enter r,h:";
cin >> r >> h;
c = 2 * 3.1415926 * r;
s1 = 3.1415926 * pow(r, 2);
s2 = 4 * 3.1415926 * pow(r, 2);
v1 = 4 * 3.1415926 * pow(r, 3) / 3;
v2 = s1 * h;
cout <<fixed<<setprecision(2)<<" 圆周长 = " <<setw(5)<< c << endl;
cout << " 圆面积 = " << setw(5) << s1 << endl;
cout << "圆球表面积 = " << setw(5) << s2 << endl;
cout << " 圆球体积 = " << setw(5) << v1 << endl;
cout << " 圆柱体积 = " << setw(5) << v2 << endl;
return 0;
}
2.有三个整数a,b,c,由键盘输入。编写程序,使用条件表达式,输出三个数中的最大值。执行结果中应有适当文字提示。
#include<iostream>
using namespace std;
int main() {
int a, b, c,max;
cout << "请输入三个整数" << endl;
cin >> a >> b >> c;
max = a > b ? a : b;
max = max > c ? max : c;
cout << "最大整数为:" << max << endl;
return 0;
}
3.输入两个正整数m和n,求其最大公约数和最小公倍数。执行结果中应有适当文字提示。提示:最大公约数最常用算法是欧几里得算法,也称为辗转相除法。
#include<iostream>
using namespace std;
int main() {
int m, n,temp;
int x, y;
int a, b;
cout << "请输入两个整数:";
cin >> m >> n;
if (m < n) {
temp = m;
m = n;
n = temp;
}
x = m;
y = n;
while (x % y != 0) {
temp = x % y;
x = y;
y = temp;
}
a = y;
b = m * n / a;
cout << m << "和" << n << "的最大公约数是" << a << endl;
cout << m << "和" << n << "的最小公倍数是" << b << endl;
return 0;
}
4.输入一行字符,分别统计出其中英文字母、空格、数字以及其他字符的个数。执行结果中应有适当文字提示。
#include<iostream>
using namespace std;
int main() {
int letters = 0,spaces = 0,numbers=0,others=0;
char x;
cout << "请输入一行字符" << endl;
while ((x = getchar())!= '\n') {
if ((x >= 'a' && x <= 'z') || (x >= 'A' && x <= 'Z')) {
letters++;
}
else if (x == ' ') {
spaces++;
}
else if (x >= '0' && x <= '9') {
numbers++;
}
else {
others++;
}
}
cout << "其中包含" << letters << "个字母," << spaces << "个空格,";
cout << numbers << "个数字," << others << "个其他字符" << endl;
return 0;
}
5.求1!+2!+3!+4!+…+20!
#include<iostream>
#include<iomanip>
using namespace std;
int main(){
float s=0;
float m=1;
for(int i=1;i<=20;i++){
m=m*i;
s=s+m;
}
cout << setiosflags(ios::scientific) << setprecision(5);
cout << "1!+2!+3!+4!+…+20!=" << s << endl;
return 0;
}
6.输出所有的“水仙花数”。所谓“水仙花数”是指一个3位数,其各位数字的立方和等于该数字本身。
#include<iostream>
using namespace std;
int main() {
int a, b, c;
cout << "水仙花数有:";
for (int i = 100; i <= 999; i++) {
c = i % 10;
b = i / 10 % 10;
a = i / 100;
if (pow(a, 3) + pow(b, 3) + pow(c, 3) == i) {
cout << i<<" ";
}
}
return 0;
}
7.有一分数序列: 2/1,3/2,5/3,8/5,13/8,21/13,…....求这个数列前20项的和。
#include<iostream>
#include<iomanip>
using namespace std;
int main() {
double a=1, b=1, temp;
double sum = 0;
for (int i = 1; i <= 20; i++) {
sum = sum +1+ a / b;
temp = a + b;
a = b;
b = temp;
}
cout << fixed << setprecision(4) << "sum=" << sum << endl;
return 0;
}
8.通过以下公式求pi的近似值(n<=1000)。
#include<iostream>
using namespace std;
int main() {
double pi = 1;
for (int i = 1; i <= 1000; i++) {
pi = pi * 4 * pow(i, 2) / (2 * i - 1) / (2 * i + 1);
}
pi = pi * 2;
cout << "pi=" << pi << endl;
return 0;
}
9.使用以下公式求Π的近似值。n/4=1-1/3+1/5-1/7......直到最后一项的绝对值小于10^(-7)为止。
#include<iostream>
#include<iomanip>
using namespace std;
int main() {
double pi = 0;
int f = 1;
for (int i = 1; i <= pow(10, 7); i = i + 2) {
pi += f*(1.00 / i);
f = -1 * f;
}
pi = pi * 4;
cout << fixed << setprecision(6)<< "pi=" << pi << endl;
return 0;
}
10.猴子吃桃问题。猴子第1天摘下若干个桃子,当即吃了一半,还不过瘾,又多吃了一个。第2天又将剩下的桃子吃掉了一半,又多吃了一个。以后的每一天都吃了前一天剩下的一半另加一个。到第10天想再吃时,发现只剩下一个桃子了。求第1天一共摘了多少个桃子。
#include<iostream>
using namespace std;
int main() {
int x = 1;
for (int i = 1; i <= 9; i++) {
x = x + 1;
x = 2 * x;
}
cout << "猴子第一天摘了" << x << "个桃子。" << endl;
return 0;
}
11.利用循环语句编程,打印出下面方框中的图形(字符之间无空格,行之间无空行)。
#include<iostream>
using namespace std;
int main() {
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 4-i; j++) {
cout << " ";
}
for (int j = 1; j <= i; j++) {
cout << " *";
}
cout << endl;
}
cout << " * * * * " << endl;
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 4 - i; j++) {
cout << " *";
}
for (int j = 1; j <= i; j++) {
cout << " ";
}
cout << endl;
}
return 0;
}
12.打印九九乘法表
#include<iostream>
#include<iomanip>
using namespace std;
int main() {
cout << "九九乘法表" << endl;
cout << endl;
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
cout << j << "×" << i << "=" << setw(4) << setiosflags(ios::left) << j * i ;
}
cout << endl;
}
cout << endl;
system("pause");
return 0;
}
13.编程实现以下图形的输出。
#include<iostream>
using namespace std;
int main() {
char x;
for (int i = 1; i <= 10; i++) {
for (int j = 1; j <= 10 - i; j++) {
cout << " ";
}
for (int j = 1; j <= 2*i-1; j++) {
x = j + 64;
cout << x;
}
for (int j = 1; j <= 10 - i; j++) {
cout << " ";
}
cout << endl;
}
return 0;
}
14.计算下列分段函数的值(要求:必须使用数学库函数实现
和
,精度要求到小数点后2位,超出数值范围的输入应有报错提示)。
#include<iostream>
#include<math.h>
#include<iomanip>
using namespace std;
int main() {
double x, y;
cout << "请输入一个x" << endl;
cin >> x;
if (x >= 0 && x < 1) {
y = 2 - x;
cout << "y=" << fixed << setprecision(2) << y << endl;
}
else if (x >= 1 && x < 3) {
y = 1 + 0.5 * x;
cout << "y=" << fixed << setprecision(2) << y << endl;
}
else if (x >= 3 && x < 6) {
y = sqrt(x);
cout << "y=" << fixed << setprecision(2) << y << endl;
}
else if (x >= 6 && x < 10) {
y = pow(x, 2);
cout << "y=" << fixed << setprecision(2) << y << endl;
}
else {
cout << "您的输入不符合取值范围" << endl;
}
return 0;
}
15.公鸡5元一只,母鸡3元一只,小鸡一元3只,100元买100鸡,列出所有可能的买法。
#include<iostream>
#include<iomanip>
using namespace std;
int main() {
const int x = 5, y = 3, z = 1;
int money = 100;
int a = 0, b = 0, c = 0;
for (a = money / x; a >= 0; a--) {
money = 100;
money = money - a * x;
for (b = money / y; b >= 0; b--) {
money = money - b * y;
c = money;
if (a + b + 3*c == 100) {
cout << "买了" << setw(2) << a << "只公鸡,";
cout << setw(2) << b << "只母鸡," << setw(2) << c << "只小鸡" << endl;
}
money = 100 - a * x;
}
}
return 0;
}
16.富翁换钱计划。有一个富翁,遇到一陌生人。陌生人找他谈了一个换钱的计划。计划如下:我每天给你十万元,而你第一天只需给我一分钱,第二天我仍给你十万元,你给我两分钱,第三天我仍给你十万元,你给我四分钱...,你每天给我的钱是前一天的两倍,直到满一个月(30天)。百万富翁很高兴,欣然接受了这个契约。
请编写程序,计算一下陌生人最后一共给了富翁多少钱?富翁又给陌生人多少钱?富翁会哭还是会笑呢?
#include<iostream>
#include<iomanip>
using namespace std;
int main() {
double x = 0.01, sum = 0;
const int y = 100000;
double i = 1.0;
int flag = 1;
for (i = 1; i <= 30; i++) {
sum = sum + x;
cout << "第" << setw(2) << (int)i << "天,陌生人得到的钱为:" << fixed << setprecision(2) << setw(10) << x << "元 ";
cout << "第" << setw(2) << (int)i << "天,富翁得到的总钱数为:" << fixed << setprecision(2) << setw(10) << i * y << "元" << endl;
x = 2 * x;
}
cout << endl;
if (((i - 1) * y - sum) < 0) {
flag = -1;
}
cout << "第30天后,陌生人得到的总钱数为:" << setw(11) << fixed<< setprecision(2) << sum << endl;
cout << "第30天后,富翁能得到的总钱数为:" << setw(11) << fixed<< setprecision(2) << (i - 1) * y << endl;
cout << "第30天后,富翁亏损的总钱数为: " << setw(11) << fixed<< setprecision(2) << ((i - 1) * y - sum)*flag << endl;
if ( flag == 1 ) {
cout << "富翁笑笑笑笑了" << endl;
}
else {
cout << "富翁哭哭哭哭了" << endl;
}
return 0;
}
17.企业发放的奖金来自利润提成。利润i低于或等于10万元的,可提成10%为奖金;利润i高于10万元,低于或等于20万元(100000<i≤200000时,低于10万元的都分按10%提成,高于10万元的部分可提成7.5% ;200000 <i≤400000时,低于20万的部分仍按上述办法提成(下同),高于20万元的部分按5%提成;400000 <i≤600000时,高于40万元的部分按3%提成;600000 <i-1000000时,高于60万元的部分按1.5%提成;i>1000000 时,超过100万元的部分按1%提成。从键盘输人当月利润i,求应发奖金总数。
#include<iostream>
#include<iomanip>
using namespace std;
int main() {
int i = 0;
double sum = 0;
cout << "请输入月利润" << endl;
cin >> i;
switch (i/100000){
case 0: sum=i*0.1; break;
case 1: sum = 100000 * 0.1 + (i - 100000) * 0.075; break;
case 2:
case 3: sum = 100000 * 0.1 + 100000 * 0.075 + (i - 200000) * 0.05; break;
case 4:
case 5: sum = 100000 * 0.175 + 200000 * 0.05 + (i - 400000) * 0.03; break;
case 6:
case 7:
case 8:
case 9: sum = 100000 * 0.175 + 200000 * 0.08+ (i - 600000) * 0.015; break;
default: sum = 100000 * 0.175 + 200000 * 0.08 + 400000 * 0.015 + (i - 1000000) * 0.01; break;
}
cout << "应发奖金总数为" << sum << "元" << endl;
return 0;
}
实验体会:
- 注意对齐时候所需要的头文件
- 注意每个循环的条件和次数
- 循环后重复使用的语句初始化不能忘记、
- 注意输出的美观