C++程序设计第2版 谭浩强习题答案文档格式.docx
《C++程序设计第2版 谭浩强习题答案文档格式.docx》由会员分享,可在线阅读,更多相关《C++程序设计第2版 谭浩强习题答案文档格式.docx(77页珍藏版)》请在冰豆网上搜索。
vq=3.0/4.0*pi*r*r*r;
vz=pi*r*r*h;
setiosflags(ios:
:
fixed)<
right)
<
setprecision
(2);
l="
setw(10)<
l<
s="
s<
sq="
sq<
vq="
vq<
vz="
vz<
}
3.#include<
intmain()
{floatc,f;
请输入一个华氏温度:
f;
c=(5.0/9.0)*(f-32);
//注意5和9要用实型表示,否则5/9值为0
摄氏温度为:
c<
};
4.#include<
{charc1,c2;
请输入两个字符c1,c2:
c1=getchar();
//将输入的第一个字符赋给c1
c2=getchar();
//将输入的第二个字符赋给c2
用putchar函数输出结果为:
putchar(c1);
putchar(c2);
用cout语句输出结果为:
9.第一种:
{inta,b,c;
pleaseenterthreeintegernumbers:
a>
b>
c;
if(a<
b)
if(b<
c)
max="
else
b;
elseif(a<
a;
}
第二种:
{inta,b,c,temp,max;
temp=(a>
b)?
a:
/*将a和b中的大者存入temp中*/
max=(temp>
c)?
temp:
/*将a和b中的大者与c比较,最大者存入max*/
max<
10.#include<
{intx,y;
enterx:
x;
if(x<
1)
{y=x;
x="
x<
y=x="
y;
elseif(x<
10)//1≤x<10
{y=2*x-1;
y=2*x-1="
else//x≥10
{y=3*x-11;
y=3*x-11="
11.#include<
{floatscore;
chargrade;
pleaseenterscoreofstudent:
score;
while(score>
100||score<
0)
{cout<
dataerror,enterdataagain."
switch(int(score/10))
{case10:
case9:
grade='
A'
break;
case8:
B'
case7:
case6:
D'
default:
grade='
E'
scoreis"
score<
gradeis"
grade<
12.#include<
{longintnum;
intindiv,ten,hundred,thousand,ten_thousand,place;
/*分别代表个位,十位,百位,千位,万位和位数*/
enteraninteger(0~99999):
num;
if(num>
9999)
place=5;
elseif(num>
999)
place=4;
99)
place=3;
9)
place=2;
elseplace=1;
place="
place<
//计算各位数字
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);
originalorder:
switch(place)
{case5:
cout<
ten_thousand<
"
thousand<
hundred<
ten<
indiv<
reverseorder:
break;
case4:
case3:
case2:
case1:
13.第一种:
{longi;
//i为利润
floatbonus,bon1,bon2,bon4,bon6,bon10;
bon1=100000*0.1;
//利润为10万元时的奖金
bon2=bon1+100000*0.075;
//利润为20万元时的奖金
bon4=bon2+100000*0.05;
//利润为40万元时的奖金
bon6=bon4+100000*0.03;
//利润为60万元时的奖金
bon10=bon6+400000*0.015;
//利润为100万元时的奖金
enteri:
i;
if(i<
=100000)
bonus=i*0.1;
//利润在10万元以内按10%提成奖金
elseif(i<
=200000)
bonus=bon1+(i-100000)*0.075;
//利润在10万元至20万时的奖金
=400000)
bonus=bon2+(i-200000)*0.05;
//利润在20万元至40万时的奖金
=600000)
bonus=bon4+(i-400000)*0.03;
//利润在40万元至60万时的奖金
=1000000)
bonus=bon6+(i-600000)*0.015;
//利润在60万元至100万时的奖金
bonus=bon10+(i-1000000)*0.01;
//利润在100万元以上时的奖金
bonus="
bonus<
{longi;
intc;
bon4=bon2+200000*0.05;
bon6=bon4+200000*0.03;
c=i/100000;
if(c>
10)c=10;
switch(c)
{case0:
case5:
case10:
14.#include<
{intt,a,b,c,d;
enterfournumbers:
c>
d;
a="
a<
b="
b<
c="
d="
d<
if(a>
{t=a;
a=b;
b=t;
a=c;
c=t;
d)
a=d;
d=t;
if(b>
{t=b;
b=c;
b=d;
{t=c;
c=d;
thesortedsequence:
"
15.#include<
{intp,r,n,m,temp;
pleaseentertwopositiveintegernumbersn,m:
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;
m=r;
HCF="
n<
LCD="
p/n<
//p是原来两个整数的乘积
16.#include<
{charc;
intletters=0,space=0,digit=0,other=0;
enteroneline:
while((c=getchar())!
='
\n'
)
{if(c>
&
&
c<
z'
||c>
Z'
letters++;
elseif(c=='
'
space++;
elseif(c>
0'
9'
digit++;
other++;
letter:
letters<
space:
space<
digit:
digit<
other:
other<
}
17.#include<
{inta,n,i=1,sn=0,tn=0;
a,n=:
n;
while(i<
=n)
{tn=tn+a;
//赋值后的tn为i个a组成数的值
sn=sn+tn;
//赋值后的sn为多项式前i项之和
a=a*10;
++i;
a+aa+aaa+...="
sn<
18.#include<
{floats=0,t=1;
intn;
for(n=1;
=20;
n++)
{
t=t*n;
//求n!
s=s+t;
//将各项累加
1!
+2!
+...+20!
="
19.#include<
{inti,j,k,n;
narcissusnumbersare:
for(n=100;
1000;
{i=n/100;
j=n/10-i*10;
k=n%10;
if(n==i*i*i+j*j*j+k*k*k)
"
20.第一种:
intmain()
{intm,s,i;
for(m=2;
m<
m++)
{s=0;
for(i=1;
i<
i++)
if((m%i)==0)s=s+i;
if(s==m)
isa完数"
itsfactorsare:
if(m%i==0)cout<
{intk[11];
inti,a,n,s;
for(a=2;
=1000;
a++)
{n=0;
s=a;
if((a%i)==0)
{n++;
s=s-i;
k[n]=i;
//将找到的因子赋给k[1]┅k[10]
if(s==0)
isa完数"
k[i]<
k[n]<
21.#include<
{inti,t,n=20;
doublea=2,b=1,s=0;
=n;
{s=s+a/b;
t=a;
a=a+b;
//将前一项分子与分母之和作为下一项的分子
b=t;
//将前一项的分子作为下一项的分母
sum="
22.#include<
{intday,x1,x2;
day=9;
x2=1;
while(day>
{x1=(x2+1)*2;
//第1天的桃子数是第2天桃子数加1后的2倍
x2=x1;
day--;
total="
x1<
23.#include<
cmath>
{floata,x0,x1;
enterapositivenumber:
//输入a的值
x0=a/2;
x1=(x0+a/x0)/2;
do
{x0=x1;
while(fabs(x0-x1)>
=1e-5);
Thesquarerootof"
is"
24.#include<
{inti,k;
for(i=0;
=3;
i++)//输出上面4行*号
{for(k=0;
k<
=2*i;
k++)
*"
//输出*号
//输出完一行*号后换行
=2;
i++)//输出下面3行*号
=4-2*i;
//输出完一行*号后换行
25.#include<
{chari,j,k;
/*i是a的对手;
j是b的对手;
k是c的对手*/
for(i='
X'
for(j='
j<
j++)
if(i!
=j)
for(k='
=k&
j!
=k)
k!
A--"
B--"
C--"
第4章
1.#include<
{inthcf(int,int);
intlcd(int,int,int);
intu,v,h,l;
u>
v;
h=hcf(u,v);
H.C.F="
h<
l=lcd(u,v,h);
L.C.D="
inthcf(intu,intv)
{intt,r;
if(v>
u)
{t=u;
u=v;
v=t;
while((r=u%v)!
=0)
{u=v;
v=r;
return(v);
intlcd(intu,intv,inth)
{return(u*v/h);
math.h>
floatx1,x2,disc,