您好,欢迎访问三七文档
C程序设计(第三版)习题答案(1-6章)谭浩强著(1)2008-04-1020:58习题答案第一章1.5#includestdio.hvoidmain(){printf(*********\nVerygood!\n*********\n);}1.6(法一)#includestdio.hmain(){inta,b,c,max;scanf(%d,%d,%d,&a,&b,&c);max=(ab)?a:b;max=(maxc)?max:c;printf(max=%d\n,max);}(法二)main(){inta,b,c,max;scanf(%d,%d,%d,&a,&b,&c);if(ab&&ac)max=a;elseif(bc)max=b;elsemax=c;printf(max=%d\n,max);}(法三)#includestdio.hvoidmain(){intmax(intx,inty,intz);inta,b,c,d;scanf(%d,%d,%d,&a,&b,&c);d=max(a,b,c);printf(max=%d\n,d);}intmax(intx,inty,intz){intA,B;if(xy)A=x;elseA=y;if(zA)B=z;elseB=A;return(B);}第三章3.3(1)(10)10=(12)8=(a)16(2)(32)10=(40)8=(20)16(3)(75)10=(113)8=(4b)16(4)(-617)10=(176627)8=(fd97)16(5)(-111)10=(177621)8=(ff91)16(6)(2483)10=(4663)8=(963)16(7)(-28654)10=(110022)8=(9012)16(8)(21003)10=(51013)8=(520b)163.6aabb(8)cc(8)abc(7)AN3.7main(){charc1='C',c2='h',c3='i',c4='n',c5='a';c1+=4,c2+=4,c3+=4,c4+=4,c5+=4;printf(%c%c%c%c%c\n,c1,c2,c3,c4,c5);}3.8main(){intc1,c2;c1=97;c2=98;printf(%c%c,c1,c2);}3.9(1)=2.5(2)=3.53.109,11,9,103.12(1)24(2)10(3)60(4)0(5)0(6)0第四章4.4main(){inta,b,c;longintu,n;floatx,y,z;charc1,c2;a=3;b=4;c=5;x=1.2;y=2.4;z=-3.6;u=51274;n=128765;c1='a';c2='b';printf(\n);printf(a=%2db=%2dc=%2d\n,a,b,c);printf(x=%8.6f,y=%8.6f,z=%9.6f\n,x,y,z);printf(x+y=%5.2fy+z=%5.2fz+x=%5.2f\n,x+y,y+z,z+x);printf(u=%6ldn=%9ld\n,u,n);printf(c1='%c'or%d(ASCII)\n,c1,c1);printf(c2='%c'or%d(ASCII)\n,c2,c2);}4.5575767.856400,-789.12396267.856400,-789.12396267.86-789.12,67.856400,-789.123962,67.856400,-789.1239626.785640e+001,-7.89e+002A,65,101,411234567,4553207,d68765535,177777,ffff,-1COMPUTER,COM4.6a=3b=7/x=8.5y=71.82/c1=Ac2=a/4.71020Aa1.5-3.75+1.4,67.8/(空3)10(空3)20Aa1.5(空1)-3.75(空1)(随意输入一个数),67.8回车4.8main(){floatpi,h,r,l,s,sq,sv,sz;pi=3.1415926;printf(inputr,h\n);scanf(%f,%f,&r,&h);l=2*pi*r;s=r*r*pi;sq=4*pi*r*r;sv=4.0/3.0*pi*r*r*r;sz=pi*r*r*h;printf(l=%6.2f\n,l);printf(s=%6.2f\n,s);printf(sq=%6.2f\n,sq);printf(vq=%6.2f\n,sv);printf(vz=%6.2f\n,sz);}4.9main(){floatc,f;scanf(%f,&f);c=(5.0/9.0)*(f-32);printf(c=%5.2f\n,c);}4.10#includestdio.hmain(){charc1,c2;scanf(%c,%c,&c1,&c2);putchar(c1);putchar(c2);printf(\n);printf(%c%c\n,c1,c2);}第五章5.3(1)0(2)1(3)1(4)0(5)15.4main(){inta,b,c;scanf(%d,%d,%d,&a,&b,&c);if(ab)if(bc)printf(max=%d\n,c);elseprintf(max=%d\n,b);elseif(ac)printf(max=%d\n,c);elseprintf(max=%d\n,a);}main(){inta,b,c,temp,max;scanf(%d,%d,%d,&a,&b,&c);temp=(ab)?a:b;max=(ctemp)?c:temp;printf(max=%d,max);}5.5main(){intx,y;scanf(%d,&x);if(x1)y=x;elseif(x10)y=2*x-1;elsey=3*x-11;printf(y=%d,y);}5.6main(){intscore,temp,logic;chargrade;logic=1;while(logic){scanf(%d,&score);if(score=0&&score=100)logic=0;}if(score==100)temp=9;elsetemp=(score-score%10)/10;switch(temp){case9:grade='A';break;case8:grade='B';break;case7:grade='C';break;case6:grade='D';break;case5:case4:case3:case2:case1:case0:grade='E';}printfscore=%d,grade=%c,score,grade);}5.7main(){longintnum;intindiv,ten,hundred,thousand,ten_thousand,place;scanf(%ld,&num);if(num9999)place=5;elseif(num999)place=4;elseif(num99)place=3;elseif(num9)place=2;elseplace=1;printf(place=%d\n,place);ten_thousand=num/10000;thousand=(num-ten_thousand*10000)/1000;hundred=(num-ten_thousand*10000-thousand*1000)/100;ten=(num-ten_thousand*10000-thousand*1000-hundred*100)/10;indiv=num-ten_thousand*10000-thousand*1000-hundred*100-ten*10;switch(place){case5:printf(%d,%d,%d,%d,%d\n,ten_thousand,thousand,hundred,ten,indiv);printf(%d,%d,%d,%d,%d\n,indiv,ten,hundred,thousand,ten_thousand);break;case4:printf(%d,%d,%d,%d\n,thousand,hundred,ten,indiv);printf(%d,%d,%d,%d\n,indiv,ten,hundred,thousand);break;case3:printf(%d,%d,%d\n,hundred,ten,indiv);printf(%d,%d,%d\n,indiv,ten,hundred);break;case2:printf(%d,%d\n,ten,indiv);printf(%d,%d\n,indiv,ten);break;case1:printf(%d\n,indiv);printf(%d\n,indiv);}}5.8main(){longi;floatbonus,bon1,bon2,bon4,bon6,bon10;bon1=100000*0.1;bon2=bon1+100000*0.075;bon4=bon2+200000*0.05;bon6=bon4+200000*0.03;bon10=bon6+400000*0.015;scanf(%ld,&i);if(i=1e5)bonus=i*0.1;elseif(i=2e5)bonus=bon1+(i-100000)*0.075;elseif(i=4e5)bonus=bon2+(i-200000)*0.05;elseif(i=6e5)bonus=bon4+(i-400000)*0.03;elseif(i=1e6)bonus=bon6+(i-600000)*0.015;elsebonus=bon10+(i-1000000)*0.01;printf(bonus=%10.2f,bonus);}main(){longi;floatbonus,bon1,bon2,bon4,bon6,bon10;intbranch;bon1=100000*0.1;bon2=bon1+100000*0.075;bon4=bon2+200000*0.05;bon6=bon4+200000*0.03;bon10=bon6+400000*0.015;scanf(%ld,&i);branch=i/100000;if(branch10)branch=10;switch(branch){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:case7case8:case9:bonus=bon6+(i-600000)*0.015;break;case10:bonus=bon10+(i-1000000)*0.01;}printf(bonus=%10.2f,bonus);}4.9main(){intt,a,b,c,d;scanf(%d,%d,%d,%d,&a,&b,&c,&d);if(ab){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;}printf(%d%d%d%d\n,a,b,c,d);}5.10main(){inth=10;floatx,y,x0=2,y0=2,d1,d2,d3,d4;scanf(%f,%f,&x,&y);d1=(x-x0)*(x-x0)+(y-y0)*(y-y0);d2=(x-x0)*
本文标题:C程序设计课后答案
链接地址:https://www.777doc.com/doc-2907651 .html