c++程序设计谭浩强课后习题答案完整版.docx
- 文档编号:4098710
- 上传时间:2022-11-27
- 格式:DOCX
- 页数:186
- 大小:49.45KB
c++程序设计谭浩强课后习题答案完整版.docx
《c++程序设计谭浩强课后习题答案完整版.docx》由会员分享,可在线阅读,更多相关《c++程序设计谭浩强课后习题答案完整版.docx(186页珍藏版)》请在冰豆网上搜索。
c++程序设计谭浩强课后习题答案完整版
第一章
1.5题
#include
usingnamespacestd;
intmain()
{
cout<<"This"<<"is";
cout<<"a"<<"C++";
cout<<"program.";
return0;
1.6题
#include
usingnamespacestd;
intmain()
{
inta,b,c;
a=10;
b=23;
c=a+b;
cout<<"a+b=";
cout< cout< return0; } 1.7七题 #include usingnamespacestd; intmain() { inta,b,c; intf(intx,inty,intz); cin>>a>>b>>c; c=f(a,b,c); cout< return0; } intf(intx,inty,intz) { intm; if(x elsem=y; if(z return(m); } 1.8题 #include usingnamespacestd; intmain() { inta,b,c; cin>>a>>b; c=a+b; cout<<"a+b="< return0; } 1.9题 #include usingnamespacestd; intmain() { inta,b,c; intadd(intx,inty); cin>>a>>b; c=add(a,b); cout<<"a+b="< return0; } intadd(intx,inty) {intz; z=x+y; return(z); } 2.3题 #include usingnamespacestd; intmain() {charc1='a',c2='b',c3='c',c4='\101',c5='\116'; cout< cout<<"\t\b"< return0; } 2.4题 #include usingnamespacestd; intmain() {charc1='C',c2='+',c3='+'; cout<<"Isay: \""< cout<<"\t\t"<<"Hesays: \"C++isveryinteresting! \""<< '\n'; return0; } 2.7题 #include usingnamespacestd; intmain() {inti,j,m,n; i=8; j=10; m=++i+j++; n=(++i)+(++j)+m; cout< return0; } 2.8题 #include usingnamespacestd; intmain() {charc1='C',c2='h',c3='i',c4='n',c5='a'; c1+=4; c2+=4; c3+=4; c4+=4; c5+=4; cout<<"passwordis: "< return0; } 3.2题 #include #include usingnamespacestd; intmain() {floath,r,l,s,sq,vq,vz; constfloatpi=3.1415926; cout<<"pleaseenterr,h: "; cin>>r>>h; 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; cout< : fixed)< : right) < (2); cout<<"l="< cout<<"s="< cout<<"sq="< cout<<"vq="< cout<<"vz="< return0; } 3.3题 #include usingnamespacestd; intmain() {floatc,f; cout<<"请输入一个华氏温度: "; cin>>f; c=(5.0/9.0)*(f-32);//注意5和9要用实型表示,否则 5/9值为0 cout<<"摄氏温度为: "< return0; }; 3.4题 #include usingnamespacestd; intmain() {charc1,c2; cout<<"请输入两个字符c1,c2: "; c1=getchar();//将输入的第一个字符赋给c1 c2=getchar();//将输入的第二个字符赋给c2 cout<<"用putchar函数输出结果为: "; putchar(c1); putchar(c2); cout< cout<<"用cout语句输出结果为: "; cout< return0; } 3.4题另一解 #include usingnamespacestd; intmain() {charc1,c2; cout<<"请输入两个字符c1,c2: "; c1=getchar();//将输入的第一个字符赋给c1 c2=getchar();//将输入的第二个字符赋给c2 cout<<"用putchar函数输出结果为: "; putchar(c1); putchar(44); putchar(c2); cout< cout<<"用cout语句输出结果为: "; cout< return0; } 3.5题 #include usingnamespacestd; intmain() {charc1,c2; inti1,i2;//定义为整型 cout<<"请输入两个整数i1,i2: "; cin>>i1>>i2; c1=i1; c2=i2; cout<<"按字符输出结果为: "< return0; } 3.8题 #include usingnamespacestd; intmain() {inta=3,b=4,c=5,x,y; cout<<(a+b>c&&b==c)< cout<<(a||b+c&&b-c)< cout<<(! (a>b)&&! c||1)< cout<<(! (x=a)&&(y=b)&&0)< cout<<(! (a+b)+c-1&&b+c/2)< return0; } 3.9题 include usingnamespacestd; intmain() {inta,b,c; cout<<"pleaseenterthreeintegernumbers: "; cin>>a>>b>>c; if(a if(b cout<<"max="< else cout<<"max="< elseif(a cout<<"max="< else cout<<"max="< cout< return0; } 3.9题另一解 #include usingnamespacestd; intmain() {inta,b,c,temp,max; cout<<"pleaseenterthreeintegernumbers: "; cin>>a>>b>>c; temp=(a>b)? a: b;/*将a和b中的大者 存入temp中*/ max=(temp>c)? temp: c;/*将a和b中的大 者与c比较,最大者存入max */ cout<<"max="< return0; } 3.10题 #include usingnamespacestd; intmain() {intx,y; cout<<"enterx: "; cin>>x; if(x<1) {y=x; cout<<"x="< } elseif(x<10)//1≤x<10 {y=2*x-1; cout<<"x="< } else//x≥10 {y=3*x-11; cout<<"x="< } cout< return0; } 3.11题 #include usingnamespacestd; intmain() {floatscore; chargrade; cout<<"pleaseenterscoreofstudent: "; cin>>score; while(score>100||score<0) {cout<<"dataerror,enterdataagain."; cin>>score; } switch(int(score/10)) {case10: case9: grade='A';break; case8: grade='B';break; case7: grade='C';break; case6: grade='D';break; default: grade='E'; } cout<<"scoreis"< "< return0; } 3.12题 #include usingnamespacestd; intmain() {longintnum; intindiv,ten,hundred,thousand,ten_thousand,place; /*分别代表个位,十位, 百位,千位,万位和位数 */ cout<<"enteraninteger(0~99999): "; cin>>num; if(num>9999) place=5; elseif(num>999) place=4; elseif(num>99) place=3; elseif(num>9) place=2; elseplace=1; cout<<"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-hund red*100)/10; indiv=(int)(num-ten_thousand*10000-thousand*1000-hun dred*100-ten*10); cout<<"originalorder: "; switch(place) {case 5: cout< <","< dl; cout<<"reverseorder: "; cout< break; case 4: cout< < cout<<"reverseorder: "; cout< break; case 3: cout< cout<<"reverseorder: "; cout< break; case2: cout< cout<<"reverseorder: "; cout< break; case1: cout< cout<<"reverseorder: "; cout< break; } return0; } 3.13题 #include usingnamespacestd; intmain() {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万元时的 奖金 cout<<"enteri: "; cin>>i; if(i<=100000) bonus=i*0.1;//利润在10万元以 内按10%提成奖金 elseif(i<=200000) bonus=bon1+(i-100000)*0.075;//利润在10万元至 20万时的奖金 elseif(i<=400000) bonus=bon2+(i-200000)*0.05;//利润在20万元至 40万时的奖金 elseif(i<=600000) bonus=bon4+(i-400000)*0.03;//利润在40万元至 60万时的奖金 elseif(i<=1000000) bonus=bon6+(i-600000)*0.015;//利润在60万元至 100万时的奖金 else bonus=bon10+(i-1000000)*0.01;//利润在100万元 以上时的奖金 cout<<"bonus="< return0; } 3.13题另一解 #include usingnamespacestd; intmain() {longi; floatbonus,bon1,bon2,bon4,bon6,bon10; intc; 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<<"enteri: "; cin>>i; c=i/100000; if(c>10)c=10; switch(c) {case0: bonus=i*0.1;break; case1: bonus=bon1+(i-100000)*0.075;break; case2: case3: bonus=bon2+(i-200000)*0.05;break; case4: case5: bonus=bon4+(i-400000)*0.03;break; case6: case7: case8: case9: bonus=bon6+(i-600000)*0.015;break; case10: bonus=bon10+(i-1000000)*0.01; } cout<<"bonus="< return0; } 3.14题 #include usingnamespacestd; intmain() {intt,a,b,c,d; cout<<"enterfournumbers: "; cin>>a>>b>>c>>d; cout<<"a="< c="< 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<<"thesortedsequence: "< cout< return0; } 3.15题 #include usingnamespacestd; intmain() {intp,r,n,m,temp; cout<<"pleaseentertwopositiveintegernumbers n,m: "; cin>>n>>m; if(n {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="< cout<<"LCD="< 乘积 return0; } 3.16题 #include usingnamespacestd; intmain() {charc; intletters=0,space=0,digit=0,other=0; cout<<"enteroneline: : "< while((c=getchar())! ='\n') {if(c>='a'&&c<='z'||c>='A'&&c<='Z') letters++; elseif(c=='') space++; elseif(c>='0'&&c<='9') digit++; else other++; } cout<<"letter: "< "< digit: "< other: "< return0; } 3.17题 #include usingnamespacestd; intmain() {inta,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+...="< return0; } 3.18题 #include usingnamespacestd; intmain() {floats=0,t=1; intn; for(n=1;n<=20;n++) { t=t*n;//求n! s=s+t;//将各项累加 } cout<<"1! +2! +...+20! ="< return0; } 3.19题 #include usingnamespacestd; intmain() {inti,j,k,n; cout<<"narcissusnumbersare: "< 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< } cout< return0; } 3.20题 #include usingnamespacestd; intmain() {constintm=1000;//定义寻找范围 intk1,k2,k3,k4,k5,k6,k7,k8,k9,k10; inti,a,n,s; for(a=2;a<=m;a++)//a是2~1000之间的整数,检查 它是否为完数 {n=0;//n用来累计a的因子的个数 s=a;//s用来存放尚未求出的因子之和, 开始时等于a for(i=1;i if(a%i==0)//如果i是a的因子 {n++;//n加1,表示新找到一个因子 s=s-i;//s减去已找到
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- c+ 程序设计 谭浩强 课后 习题 答案 完整版
![提示](https://static.bdocx.com/images/bang_tan.gif)