【解】略。
【解】
#include
#include
using namespace std;
int main()
{
float h, r, l, s, sq, vq, vz;
const float pi = 3.1415926;
cout << "please enter r,h:"; //要求输入圆半径r和圆柱高h
cin >> r >> h; //输入r和h
l = 2 * pi*r; //计算圆周长l
s = r * r*pi; //计算圆面积s
sq = 4 * pi*r*r; //计算圆球表面积sq
vq = 4.0 / 3.0*pi*r*r*r; //计算圆球体积vq
vz = pi * r*r*h; //计算圆柱体积vz
cout << setiosflags(ios::fixed) << setiosflags(ios::right) << setprecision(2);
//设置输出格式
cout << " l= " << setw(10) << l << endl; //输出圆周长l, 指定字段宽度为10
cout << " s= " << setw(10) << s << endl; //输出圆面积s
cout << "sq=" << setw(10) << sq << endl; //输出圆球表面积sq
cout << " vq=" << setw(10) << vq << endl; //输出圆球体积vq
cout << " vz=" << setw(10) << vz << endl; //输出圆柱体积vz
return 0;
}
运行结果:
//please enter r, h: 1.5 3↙
//l = 9.42
//s = 7.07
//sq = 28.27
//vq = 14.14
//vz = 21.20
【解】
#include
using namespace std;
int main()
{
float c, f;
cout << "请输入一个华氏温度: ";
cin >> f;
c = (5.0 / 9.0) * (f - 32); //注意5和9要用实型表示, 否则5/9值为0
cout << "摄氏温度为: " << c << endl;
return 0;
}
运行结果:
//请输入一个华氏温度: 56↙
//摄氏温度为 : 13.33
【解】
#include
using namespace std;
int main()
{
char c1, c2;
cout << "请输入两个字符c1,c2:";
c1 = getchar(); //将输入的第1个字符赋给c1
c2 = getchar(); //将输入的第2个字符赋给c2
cout << "用putchar函数输出结果为:";
putchar(c1);
putchar(c2);
cout << endl;
cout << "用cout语句输出结果为:";
cout << c1 << c2 << endl;
return 0;
}
运行结果:
//请输入两个字符c1, c2:ab↙
//用putchar函数输出结果为 : ab
//用cout语句输出结果为 : ab
【解】略。
【解】略。
【解】略。
【解】略。
【解】
//(1)方法一:用if语句。
#include
using namespace std;
int main()
{
int a, b, c;
cout << "please enter three integer numbers:";
cin >> a >> b >> c;
if (a < b)
if (b < c)
cout << "max=" << c;
else
cout << "max=" << b;
else if (a < c)
cout << "max=" << c;
else
cout << "max=" << a;
cout << endl;
return 0;
}
//(2)方法二:使用条件表达式,可以使程序更加简明、清晰。
#include
using namespace std;
int main()
{
int a, b, c, temp, max;
cout << "please enter three integer numbers:";
cin >> a >> b >> c;
temp = (a > b) ? a : b; //将a和b中的大者存入temp中
max = (temp > c) ? temp : c; //将a和b中的大者与c比较, 最大者存入max中
cout << "max=" << max << endl;
return 0;
}
运行结果:
//please enter three integer numbers :: 121 –34 40↙
//max = 121
【解】
#include < iostream >
using namespace std;
int main()
{
int x, y;
cout << "enter x:";
cin >> x;
if (x < 1)
{
y = x;
cout << "x=" << x << ", y=x=" << y;
}
else if (x < 10) // 1≤x<10
{
y = 2 * x - 1;
cout << "x=" << x << ", y=2*x–1=" << y;
}
else // x≥10
{
y = 3 * x - 11;
cout << "x=" << x << ", y=3*x–11=" << y;
}
cout << endl;
return 0;
}
运行结果:
//① enter x : 4↙
//x = 4, y = 2 * x–1 = 7
//② enter x : –1↙
//x = –1, y = x = –1
//③ enter x : 20↙
//x = 20, y = 3 * x–11 = 49
【解】
#include
using namespace std;
int main()
{
float score;
char grade;
cout << "please enter score of student:";
cin >> score;
while (score > 100 || score < 0)
{
cout << "data error,enter data again.";
cin >> score;
}
switch (int(score / 10))
{
case 10:
case 9: grade = 'A'; break;
case 8: grade = 'B'; break;
case 7: grade = 'C'; break;
case 6: grade = 'D'; break;
default:grade = 'E';
}
cout << "score is " << score << ", grade is " << grade << endl;
return 0;
}
运行结果:
//① please enter score of student : 90.5↙
//score is 90.5, grade is A
//② please enter score of student : 45.5↙
//score is 59.5, grade is E
【解】
#include
using namespace std;
int main()
{
long int num;
int indiv, ten, hundred, thousand, ten_thousand, place;
//分别代表个位、十位、百位、千位、万位和位数
cout << "enter an integer(0~99999):";
cin >> num;
if (num > 9999)
place = 5;
else if (num > 999)
place = 4;
else if (num > 99)
place = 3;
else if (num > 9)
place = 2;
else place = 1;
cout << "place=" << place << endl;
//计算各位数字
ten_thousand = num / 10000;
thousand = (int)(num - ten_thousand * 10000) / 1000;
hundred = (int)(num - ten_thousand * 10000 - thousand * 1000) / 100;
ten = (int)(num - ten_thousand * 10000 - thousand * 1000 - hundred * 100) / 10;
indiv = (int)(num - ten_thousand * 10000 - thousand * 1000 - hundred * 100 - ten * 10);
cout << "original order:";
switch (place)
{
case 5: cout << ten_thousand << "," << thousand << "," << hundred << "," << ten << "," << indiv << endl;
cout << "reverse order:";
cout << indiv << ten << hundred << thousand << ten_thousand << endl;
break;
case 4: cout << thousand << "," << hundred << "," << ten << "," << indiv << endl;
cout << "reverse order:";
cout << indiv << ten << hundred << thousand << endl;
break;
case 3: cout << hundred << "," << ten << "," << indiv << endl;
cout << "reverse order:";
cout << indiv << ten << hundred << endl;
break;
case 2: cout << ten << "," << indiv << endl;
cout << "reverse order:";
cout << indiv << ten << endl;
break;
case 1: cout << indiv << endl;
cout << "vreverse order : ";
cout << indiv << endl;
break;
}
return 0;
}
运行结果:
//enter an integer(0~99999) :98765↙
//place = 5
//original order : 9, 8, 7, 6, 5
//reverse order : 56789
【解】
//(1)用if语句
#include
using namespace std;
int main()
{
long i; //i为利润
float bonus, bon1, bon2, bon4, bon6, bon10;
bon1 = 100000 * 0.1; //利润为10万元时的奖金
bon2 = bon1 + 100000 * 0.075; //利润为20万元时的奖金
bon4 = bon2 + 200000 * 0.05; //利润为40万元时的奖金
bon6 = bon4 + 200000 * 0.03; //利润为60万元时的奖金
bon10 = bon6 + 400000 * 0.015; //利润为100万元时的奖金
cout << "enter i:";
cin >> i;
if (i <= 100000)
bonus = i * 0.1; //利润在10万元以内按10%提成奖金
else if (i <= 200000)
bonus = bon1 + (i - 100000)*0.075; //利润在10万元至20万元时的奖金
else if (i <= 400000)
bonus = bon2 + (i - 200000)*0.05; //利润在20万元至40万元时的奖金
else if (i <= 600000)
bonus = bon4 + (i - 400000)*0.03; //利润在40万元至60万元时的奖金
else if (i <= 1000000)
bonus = bon6 + (i - 600000)*0.015; //利润在60万元至100万元时的奖金
else
bonus = bon10 + (i - 1000000)*0.01; //利润在100万元以上时的奖金
cout << "bonus=" << bonus << endl;
return 0;
}
//(2)用switch语句
#include
using namespace std;
int main()
{
long i;
float bonus, bon1, bon2, bon4, bon6, bon10;
int c;
bon1 = 100000 * 0.1;
bon2 = bon1 + 100000 * 0.075;
bon4 = bon2 + 200000 * 0.05;
bon6 = bon4 + 200000 * 0.03;
bon10 = bon6 + 400000 * 0.015;
cout << "enter i:";
cin >> i;
c = i / 100000;
if (c > 10) c = 10;
switch (c)
{
case 0: bonus = i * 0.1; break;
case 1: bonus = bon1 + (i - 100000)*0.075; break;
case 2:
case 3: bonus = bon2 + (i - 200000)*0.05; break;
case 4:
case 5: bonus = bon4 + (i - 400000)*0.03; break;
case 6:
case 7:
case 8:
case 9: bonus = bon6 + (i - 600000)*0.015; break;
case 10: bonus = bon10 + (i - 1000000)*0.01;
}
cout << "bonus=" << bonus << endl;
return 0;
}
运行结果:
//enter i : 234000↙
//bonus = 19200
【解】
#include
using namespace std;
int main()
{
int t, a, b, c, d;
cout << "enter four numbers:";
cin >> a >> b >> c >> d;
cout << "a=" << a << ", b=" << b << ", c=" << c << ",d=" << d << endl;
if (a > b)
{
t = a; a = b; b = t;
}
if (a > c)
{
t = a; a = c; c = t;
}
if (a > d)
{
t = a; a = d; d = t;
}
if (b > c)
{
t = b; b = c; c = t;
}
if (b > d)
{
t = b; b = d; d = t;
}
if (c > d)
{
t = c; c = d; d = t;
}
cout << "the sorted sequence:" << endl;
cout << a << ", " << b << ", " << c << ", " << d << endl;
return 0;
}
运行结果:
//enter four numbers : 6 8 1 4↙
//a = 6, b = 8, c = 1, d = 4
//the sorted sequence :
//1, 4, 6, 8
【解】
#include
using namespace std;
int main()
{
int p, r, n, m, temp;
cout << "please enter two positive integer numbers n,m:";
cin >> n >> m;
if (n < m)
{
temp = n;
n = m;
m = temp; //把较大数放在n中, 较小数放在m中
}
p = n * m; //先将n和m的乘积保存在p中, 以便求最小公倍数时使用
while (m != 0) //求n和m的最大公约数
{
r = n % m;
n = m;
m = r;
}
cout << "HCF=" << n << endl;
cout << "LCD=" << p / n << endl; // p是原来两个整数的乘积
return 0;
}
运行结果:
//please enter two positive integer numbers n, m:15 9 ↙
//HCF = 3
//LCD = 45
【解】
#include
using namespace std;
int main()
{
char c;
int letters = 0, space = 0, digit = 0, other = 0;
cout << "enter one line:" << endl;
while ((c = getchar()) != '\n')
{
if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z')
letters++;
else if (c == ' ')
space++;
else if (c >= '0' && c <= '9')
digit++;
else
other++;
}
cout << " letter:" << letters << ",space:" << space << ",digit:" << digit << ",other:" << other << endl;
return 0;
}
运行结果:
//enter one line :
//My teacher's address is "#123 Beijing Road,Shanghai".
//letter : 38, space : 6, digit : 3, other : 6
【解】
#include
using namespace std;
int main()
{
int a, n, i = 1, sn = 0, tn = 0;
cout << "a,n=:";
cin >> a >> n;
while (i <= n)
{
tn = tn + a; //赋值后的tn为i个a组成数的值
sn = sn + tn; //赋值后的sn为多项式前i项之和
a = a * 10;
++i;
}
cout << "a+aa+aaa+…=" << sn << endl;
return 0;
}
运行结果:
//a, n: 2 5↙
//a + aa + aaa + … = 24690
【解】
#include
using namespace std;
int main()
{
float s = 0, t = 1;
int n;
for (n = 1; n <= 20; n++)
{
t = t * n; //求n!
s = s + t; //将各项累加
}
cout << "1!+2!+…+20!=" << s << endl;
return 0;
}
运行结果:
//1!+ 2!+ … + 20 != 2.56133e+018
【解】
#include
using namespace std;
int main()
{
int i, j, k, n;
cout << "narcissus numbers are:" << endl;
for (n = 100; n < 1000; n++)
{
i = n / 100;
j = n / 10 - i * 10;
k = n % 10;
if (n == i * i * i + j * j * j + k * k * k)
cout << n << " ";
}
cout << endl;
return 0;
}
运行结果:
//narcissus numbers are : 153 370 371 407
【解】
//方法一:
#include < iostream >
using namespace std;
int main()
{
const int m = 1000; //定义寻找范围
int k1, k2, k3, k4, k5, k6, k7, k8, k9, k10;
int i, a, n, s;
for (a = 2; a <= m; a++) // a是2~1000的整数, 检查它是否为完数
{
n = 0; //n用来累计a的因子的个数
s = a; //s用来存放尚未求出的因子之和, 开始时等于a
for (i = 1; i < a; i++) //检查i是否为a的因子
if (a%i == 0) //如果i是a的因子
{
n++; //n加1,表示新找到一个因子
s = s - i; //s减去已找到的因子, s的新值是尚未求出的因子之和
switch (n) //将找到的因子赋给k1,…, k10
{
case 1:
k1 = i; break; //找出的第1个因子赋给k1
case 2:
k2 = i; break; //找出的第2个因子赋给k2
case 3:
k3 = i; break; //找出的第3个因子赋给k3
case 4:
k4 = i; break; //找出的第4个因子赋给k4
case 5:
k5 = i; break; //找出的第5个因子赋给k5
case 6:
k6 = i; break; //找出的第6个因子赋给k6
case 7:
k7 = i; break; //找出的第7个因子赋给k7
case 8:
k8 = i; break; //找出的第8个因子赋给k8
case 9:
k9 = i; break; //找出的第9个因子赋给k9
case 10:
k10 = i; break; //找出的第10个因子赋给k10
}
}
if (s == 0) //s=0表示全部因子都已找到了
{
cout << a << " is a 完数" << endl;
cout << "its factors are:";
if (n > 1) cout << k1 << "," << k2; //n>1表示a至少有2个因子
if (n > 2) cout << "," << k3; //n>2表示至少有3个因子, 故应再输出一个因子
if (n > 3) cout << "," << k4; //n>3表示至少有4个因子, 故应再输出一个因子
if (n > 4) cout << "," << k5; //以下类似
if (n > 5) cout << "," << k6;
if (n > 6) cout << "," << k7;
if (n > 7) cout << "," << k8;
if (n > 8) cout << "," << k9;
if (n > 9) cout << "," << k10;
cout << endl << endl;
}
}
return 0;
}
//方法二:
#include
using namespace std;
int main()
{
int m, s, i;
for (m = 2; m < 1000; m++)
{
s = 0;
for (i = 1; i < m; i++)
if ((m % i) == 0) s = s + i;
if (s == m)
{
cout << m << "is a完数" << endl;
cout << "its factors are:";
for (i = 1; i < m; i++)
if (m % i == 0) cout << i << " ";
cout << endl;
}
}
return 0;
}
//方法三:此题用数组方法更为简单。
#include
using namespace std;
int main()
{
int k[11];
int i, a, n, s;
for (a = 2; a <= 1000; a++)
{
n = 0;
s = a;
for (i = 1; i < a; i++)
if ((a % i) == 0)
{
n++;
s = s - i;
k[n] = i; //将找到的因子赋给k[1],…, k[10]
}
if (s == 0)
{
cout << a << " is a 完数" << endl;
cout << "its factors are: ";
for (i = 1; i < n; i++)
cout << k[i] << " ";
cout << k[n] << endl;
}
}
return 0;
}
运行结果:
//6 is a 完数
//its factors are : 1, 2, 3
//28 is a 完数
//its factors are : 1, 2, 4, 7, 14
//496 is a 完数
//its factors are : 1, 2, 4, 8, 16, 31, 62, 124, 248
【解】
#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;
}
运行结果:
//sum = 32.6603
【解】
#include
using namespace std;
int main()
{
int day, x1, x2;
day = 9;
x2 = 1;
while (day > 0)
{
x1 = (x2 + 1) * 2; //第1天的桃子数是第2天桃子数加1后的2倍
x2 = x1;
day--;
}
cout << "total=" << x1 << endl;
return 0;
}
运行结果:
//total = 1534
【解】
#include
#include
using namespace std;
int main()
{
float a, x0, x1;
cout << "enter a positive number:";
cin >> a; // 输入a的值
x0 = a / 2;
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;
}
运行结果:
//enter a positive number : 2↙
//The square root of 2 is 1.41421
【解】
#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;
}
运行结果:
//*
//***
//*****
//*******
//*****
//***
//*
【解】
#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;
}
运行结果:
//A– – –Z B– – –X C– – –Y