C程序设计第三版课后习题答案谭浩强著1.docx
- 文档编号:4689244
- 上传时间:2022-12-07
- 格式:DOCX
- 页数:73
- 大小:28.56KB
C程序设计第三版课后习题答案谭浩强著1.docx
《C程序设计第三版课后习题答案谭浩强著1.docx》由会员分享,可在线阅读,更多相关《C程序设计第三版课后习题答案谭浩强著1.docx(73页珍藏版)》请在冰豆网上搜索。
C程序设计第三版课后习题答案谭浩强著1
C程序设计(第三版)习题答案(1-6章)谭浩强著
习题答案
第一章
1.5
#include
voidmain()
{
printf("*********\n Verygood!
\n*********\n");
}
1.6
(法一)
#include
main()
{
inta,b,c,max;
scanf("%d,%d,%d",&a,&b,&c);
max=(a>b)?
a:
b;
max=(max>c)?
max:
c;
printf("max=%d\n",max);
}
(法二)
main()
{
inta,b,c,max;
scanf("%d,%d,%d",&a,&b,&c);
if(a>b&&a>c)
max=a;
elseif(b>c)
max=b;
else
max=c;
printf("max=%d\n",max);
}
(法三)
#include
voidmain()
{
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(x>y)A=x;
elseA=y;
if(z>A)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)16
3.6
aabb (8)cc (8)abc
(7)AN
3.7
main()
{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.8
main()
{intc1,c2;
c1=97;c2=98;
printf("%c%c",c1,c2);
}
3.9
(1)=2.5
(2)=3.5
3.10
9,11,9,10
3.12
(1)24
(2)10(3)60(4)0(5)0(6)0
第四章
4.4
main()
{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.5
57
57
67.856400,-789.123962
67.856400,-789.123962
67.86-789.12,67.856400,-789.123962,67.856400,-789.123962
6.785640e+001,-7.89e+002
A,65,101,41
1234567,4553207,d687
65535,177777,ffff,-1
COMPUTER,COM
4.6
a=3b=7/
x=8.5y=71.82/
c1=Ac2=a/
4.7
10 20Aa1.5-3.75+1.4,67.8/
(空3)10(空3)20Aa1.5(空1)-3.75(空1)(随意输入一个数),67.8回车
4.8
main()
{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.9
main()
{floatc,f;
scanf("%f",&f);
c=(5.0/9.0)*(f-32);
printf("c=%5.2f\n",c);
}
4.10
#include"stdio.h"
main()
{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)1
5.4
main()
{inta,b,c;
scanf("%d,%d,%d",&a,&b,&c);
if(a
if(b printf("max=%d\n",c); else printf("max=%d\n",b); elseif(a printf("max=%d\n",c); else printf("max=%d\n",a); } main() {inta,b,c,temp,max; scanf("%d,%d,%d",&a,&b,&c); temp=(a>b)? a: b; max=(c>temp)? c: temp; printf("max=%d",max); } 5.5 main() {intx,y; scanf("%d",&x); if(x<1)y=x; elseif(x<10)y=2*x-1; elsey=3*x-11; printf("y=%d",y); } 5.6 main() {intscore,temp,logic; chargrade; logic=1; while(logic) {scanf("%d",&score); if(score>=0&&score<=100)logic=0; } if(score==100) temp=9; else temp=(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'; } printf"score=%d,grade=%c",score,grade); } 5.7 main() {longintnum; intindiv,ten,hundred,thousand,ten_thousand,place; scanf("%ld",&num); if(num>9999)place=5; elseif(num>999)place=4; elseif(num>99)place=3; elseif(num>9)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.8 main() {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(branch>10)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: case7 case8: case9: bonus=bon6+(i-600000)*0.015;break; case10: bonus=bon10+(i-1000000)*0.01; } printf("bonus=%10.2f",bonus); } 4.9 main() {intt,a,b,c,d; scanf("%d,%d,%d,%d",&a,&b,&c,&d); 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;} printf("%d%d%d%d\n",a,b,c,d); } 5.10 main() {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)*(x-x0)+(y+y0)*(y+y0); d3=(x+x0)*(x+x0)+(y-y0)*(y-y0); d4=(x+x0)*(x+x0)+(y+y0)*(y+y0); if(d1>1&&d2>1&&d3>1&&d4>1)h=0; printf("h=%d",h); } 第六章循环控制 6.1 main() {inta,b,num1,num2,temp; scanf("%d,%d",&num1,&num2); if(num1 a=num1;b=num2; while(b! =0) {temp=a%b; a=b; b=temp;} printf("%d\n",a); printf("%d\n",num1*num2/a); } 6.2 #include"stdio.h" main() {charc; intletters=0,space=0,digit=0,other=0; while((c=getchar())! ='\n') {if(c>='a'&&c<='z'||c>='A'&&c<='Z')letters++; elseif(c=='')space++; elseif(c>='0'&&c<='9')digit++; elseother++; } printf("letters=%d\nspace=%d\ndigit=%d\nother=%d\n",letters,space,digit,other); } 6.3 main() {inta,n,count=1,sn=0,tn=0; scanf("%d,%d",&a,&n); while(count<=n) {tn+=a; sn+=tn; a*=10; ++count; } printf("a+aa+aaa+...=%d\n",sn); } 6.4 main() {floatn,s=0,t=1; for(n=1;n<=20;n++) {t*=n; s+=t; } printf("s=%e\n",s); } 6.5 main() {intN1=100,N2=50,N3=10; floatk; floats1=0,s2=0,s3=0; for(k=1;k<=N1;k++)s1+=k; for(k=1;k<=N2;k++)s2+=k*k; for(k=1;k<=N3;k++)s3+=1/k; printf("s=%8.2f\n",s1+s2+s3); } 6.6 main() {inti,j,k,n; for(n=100;n<1000;n++) {i=n/100; j=n/10-i*10; k=n%10; if(i*100+j*10+k==i*i*i+j*j*j+k*k*k) printf("n=%d\n",n); } } 6.7 #defineM1000 main() {intk0,k1,k2,k3,k4,k5,k6,k7,k8,k9; inti,j,n,s; for(j=2;j<=M;j++) {n=0; s=j; for(i=1;i {if((j%i)==0) {n++; s=s-i; switch(n) {case1: k0=i;break; case2: k1=i;break; case3: k2=i;break; case4: k3=i;break; case5: k4=i;break; case6: k5=i;break; case7: k6=i;break; case8: k7=i;break; case9: k8=i;break; case10: k9=i;break; } } } if(s==0) {printf("j=%d\n",j); if(n>1)printf("%d,%d",k0,k1); if(n>2)printf(",%d",k2); if(n>3)printf(",%d",k3); if(n>4)printf(",%d",k4); if(n>5)printf(",%d",k5); if(n>6)printf(",%d",k6); if(n>7)printf(",%d",k7); if(n>8)printf(",%d",k8); if(n>9)printf(",%d\n",k9); } } } main() {staticintk[10]; inti,j,n,s; for(j=2;j<=1000;j++) {n=-1; s=j; for(i=1;i {if((j%i)==0) {n++; s=s-i; k[n]=i; } } if(s==0) {printf("j=%d\n",j); for(i=0;i printf("%d,",k[i]); printf("%d\n",k[n]); } } } 6.8 main() {intn,t,number=20; floata=2;b=1;s=0; for(n=1;n<=number;n++) {s=s+a/b; t=a,a=a+b,b=t; } printf("s=%9.6f\n",s); } 6.9 main() {floatsn=100.0,hn=sn/2; intn; for(n=2;n<=10;n++) {sn=sn+2*hn; hn=hn/2; } printf("sn=%f\n",sn); printf("hn=%f\n",hn); } 6.10 main() {intday,x1,x2; day=9; x2=1; while(day>0) {x1=(x2+1)*2; x2=x1; day--; } printf("x1=%d\n",x1); } 6.11 #include"math.h" main() {floata,xn0,xn1; scanf("%f",&a); xn0=a/2; xn1=(xn0+a/xn0)/2; do {xn0=xn1; xn1=(xn0+a/xn0)/2; } while(fabs(xn0-xn1)>=1e-5); printf("a=%5.2f\n,xn1=%8.2f\n",a,xn1); } 6.12 #include"math.h" main() {floatx,x0,f,f1; x=1.5; do {x0=x; f=((2*x0-4)*x0+3)*x0-6; f1=(6*x0-8)*x0+3; x=x0-f/f1; } while(fabs(x-x0)>=1e-5); printf("x=%6.2f\n",x); } 6.13 #include"math.h" main() {floatx0,x1,x2,fx0,fx1,fx2; do {scanf("%f,%f",&x1,&x2); fx1=x1*((2*x1-4)*x1+3)-6; fx2=x2*((2*x2-4)*x2+3)-6; } while(fx1*fx2>0); do {x0=(x1+x2)/2; fx0=x0*((2*x0-4)*x0+3)-6; if((fx0*fx1)<0) {x2=x0; fx2=fx0; } else
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 程序设计 第三 课后 习题 答案 谭浩强著
![提示](https://static.bdocx.com/images/bang_tan.gif)