1、c+程序设计谭浩强课后习题答案第一章1.5题#include using namespace std;int main() coutThisis; coutaC+; coutprogram.; return 0;1.6题#include using namespace std;int main() int a,b,c; a=10; b=23; c=a+b; couta+b=; coutc; coutendl; return 0;1.7七题#include using namespace std;int main() int a,b,c; int f(int x,int y,int z); ci
2、nabc; c=f(a,b,c); coutcendl; return 0;int f(int x,int y,int z) int m; if (xy) m=x; else m=y; if (zm) m=z; return(m); 1.8题#include using namespace std;int main() int a,b,c; cinab; c=a+b; couta+b=a+bendl; return 0;1.9题#include using namespace std;int main() int a,b,c; int add(int x,int y); cinab; c=ad
3、d(a,b); couta+b=cendl; return 0;int add(int x,int y)int z; z=x+y; return(z);2.3题#include using namespace std;int main() char c1=a,c2=b,c3=c,c4=101,c5=116; coutc1c2c3n; couttbc4tc5n; return 0;2.4题#include using namespace std;int main() char c1=C,c2=+,c3=+; coutI say: c1c2c3; coutttHe says: C+ is very
4、 interesting! n; return 0;2.7题#include using namespace std;int main()int i,j,m,n; i=8; j=10; m=+i+j+; n=(+i)+(+j)+m; coutitjtmtnendl; return 0;2.8题#include using namespace std;int main()char c1=C, c2=h, c3=i, c4=n, c5=a; c1+=4; c2+=4; c3+=4; c4+=4; c5+=4; coutpassword is:c1c2c3c4c5endl; return 0;3.2
5、题#include #include using namespace std;int main ( )float h,r,l,s,sq,vq,vz; const float pi=3.1415926; coutrh; l=2*pi*r; s=r*r*pi; sq=4*pi*r*r; vq=3.0/4.0*pi*r*r*r; vz=pi*r*r*h; coutsetiosflags(ios:fixed)setiosflags(ios:right) setprecision(2); coutl= setw(10)lendl; couts= setw(10)sendl; coutsq=setw(10
6、)sqendl; coutvq=setw(10)vqendl; coutvz=setw(10)vzendl; return 0; 3.3题#include using namespace std;int main ()float c,f; coutf; c=(5.0/9.0)*(f-32); /注意5和9要用实型表示,否则5/9值为0 cout摄氏温度为:cendl; return 0;3.4题#include using namespace std;int main ( )char c1,c2; cout请输入两个字符c1,c2:; c1=getchar(); /将输入的第一个字符赋给c1
7、c2=getchar(); /将输入的第二个字符赋给c2 cout用putchar函数输出结果为:; putchar(c1); putchar(c2); coutendl; cout用cout语句输出结果为:; coutc1c2endl; return 0;3.4题另一解#include using namespace std;int main ( )char c1,c2; cout请输入两个字符c1,c2:; c1=getchar(); /将输入的第一个字符赋给c1 c2=getchar(); /将输入的第二个字符赋给c2 cout用putchar函数输出结果为:; putchar(c1);
8、 putchar(44); putchar(c2); coutendl; cout用cout语句输出结果为:; coutc1,c2endl; return 0;3.5题#include using namespace std;int main ( )char c1,c2; int i1,i2; /定义为整型 couti1i2; c1=i1; c2=i2; cout按字符输出结果为:c1 , c2endl; return 0;3.8题#include using namespace std;int main ( ) int a=3,b=4,c=5,x,y; coutc & b=c)endl; c
9、out(a|b+c & b-c)endl; coutb) & !c|1)endl; cout(!(x=a) & (y=b) & 0)endl; cout(!(a+b)+c-1 & b+c/2)endl; return 0; 3.9题include using namespace std;int main ( ) int a,b,c; coutabc; if(ab) if(bc) coutmax=c; else coutmax=b; else if (ac) coutmax=c; else coutmax=a; coutendl; return 0; 3.9题另一解#include using
10、namespace std;int main ( ) int a,b,c,temp,max ; coutabc; temp=(ab)?a:b; /* 将a和b中的大者存入temp中 */ max=(tempc)?temp:c; /* 将a和b中的大者与c比较,最大者存入max */ coutmax=maxendl; return 0; 3.10题#include using namespace std;int main ( ) int x,y; coutx; if (x1) y=x; coutx=x, y=x=y; else if (x10) / 1x10 y=2*x-1; coutx=x,
11、y=2*x-1=y; else / x10 y=3*x-11; coutx=x, y=3*x-11=y; coutendl; return 0;3.11题#include using namespace std;int main () float score; char grade; coutscore; while (score100|score0) coutscore; switch(int(score/10) case 10: case 9: grade=A;break; case 8: grade=B;break; case 7: grade=C;break; case 6: grad
12、e=D;break; default:grade=E; coutscore is score, grade is gradeendl; return 0;3.12题#include using namespace std;int main ()long int num; int indiv,ten,hundred,thousand,ten_thousand,place; /*分别代表个位,十位,百位,千位,万位和位数 */ coutnum; if (num9999) place=5; else if (num999) place=4; else if (num99) place=3; else
13、 if (num9) place=2; else place=1; coutplace=placeendl; /计算各位数字 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*100
14、0-hundred*100-ten*10); coutoriginal order:; switch(place) case 5:coutten_thousand,thousand,hundred,ten,indiven dl; coutreverse order:; coutindivtenhundredthousandten_thousandendl; break; case 4:coutthousand,hundred,ten,indivendl; coutreverse order:; coutindivtenhundredthousandendl; break; case 3:cou
15、thundred,ten,indivendl; coutreverse order:; coutindivtenhundredendl; break; case 2:coutten,indivendl; coutreverse order:; coutindivtenendl; break; case 1:coutindivendl; coutreverse order:; coutindivendl; break; return 0; 3.13题#include using namespace std;int main () long i; /i为利润 float bonus,bon1,bo
16、n2,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万元时的奖金 couti; if (i=100000) bonus=i*0.1; /利润在10万元以内按10%提成奖金 else if (i=200000) bonus=bon1+(i-100000)*0.075; /
17、利润在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万元以上时的奖金 coutbonus=bonusendl; return 0; 3.13题另一
18、解#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; couti; c=i/100000; if (c10) c=10; switch(c) case 0: bonus=i*0.1; break; case 1: bonus=bon1+(i
19、-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; coutbonus=bonusendl; return 0;3.14题#include using namespace std;int ma
20、in ()int t,a,b,c,d; coutabcd; couta=a, b=b, c=c,d=db) t=a;a=b;b=t; if (ac) t=a; a=c; c=t; if (ad) t=a; a=d; d=t; if (bc) t=b; b=c; c=t; if (bd) t=b; b=d; d=t; if (cd) t=c; c=d; d=t; coutthe sorted sequence:endl; couta, b, c, dendl; return 0; 3.15题#include using namespace std;int main ()int p,r,n,m,t
21、emp; coutnm; if (nm) 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; coutHCF=nendl; coutLCD=p/nendl; / p是原来两个整数的乘积 return 0; 3.16题#include using namespace std;int main ()char c; int letters=0,space=0,digit=0,other=0; coutenter one lin
22、e:=a & c=A & c=0 & c=9) digit+; else other+; coutletter:letters, space:space, digit:digit, other:otherendl; return 0; 3.17题#include using namespace std;int main ()int a,n,i=1,sn=0,tn=0; coutan; while (i=n) tn=tn+a; /赋值后的tn为i个a组成数的值 sn=sn+tn; /赋值后的sn为多项式前i项之和 a=a*10; +i; couta+aa+aaa+.=snendl; return
23、 0; 3.18题#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; / 将各项累加 cout1!+2!+.+20!=sendl; return 0; 3.19题#include using namespace std;int main ()int i,j,k,n; coutnarcissus numbers are:endl; for (n=100;n1000;n+) i=n/100; j=n/10-i*10; k=n%10; if (n = i*i*i + j*j*j + k*k*k) coutn ; coutendl; return 0; 3.20题#include 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=
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1