- 2022-04-07 发布 |
- 37.5 KB |
- 42页
申明敬告: 本站不保证该用户上传的文档完整性,不预览、不比对内容而直接下载产生的反悔问题本站不予受理。
文档介绍
C程序设计(第三版,C语言)课后习题答案
C程序设计(第三版)习题答案(1-6章)谭浩强-著习题答案第一章1.5#include
voidmain(){printf("*********n Verygood!n*********n");}1.6(法一)#includemain(){inta,b,c,max;scanf("%d,%d,%d",&a,&b,&c);max=(a>b)?a:b;max=(max>c)?max:c;printf("max=%dn",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=%dn",max);}(法三)#includevoidmain(){intmax(intx,inty,intz);inta,b,c,d;scanf("%d,%d,%d",&a,&b,&c);d=max(a,b,c);printf("max=%dn",d);}intmax(intx,inty,intz){intA,B;if(x>y)A=x;elsenA=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)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%cn",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,c2n;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=%2dn",a,b,c);printf("x=%8.6f,y=%8.6f,z=%9.6fn",x,y,z);printf("x+y=%5.2fy+z=%5.2fz+x=%5.2fn",x+y,y+z,z+x);printf("u=%6ldn=%9ldn",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.123962 67.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.7 10 20Aa1.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,hn");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.2fn",l);printf("s=%6.2fn",s);printf("sq=%6.2fn",sq);printf("vq=%6.2fn",sv);printf("vz=%6.2fn",sz);}4.9main(){floatc,f;scanf("%f",&f);c=(5.0/9.0)*(f-32);printf("c=%5.2fn",c);}n4.10#include"stdio.h"main(){charc1,c2;scanf("%c,%c",&c1,&c2);putchar(c1);putchar(c2);printf("n");printf("%c%cn",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)?a:b;max=(c>temp)?c:temp;printf("max=%d",max);}5.5main(){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.6main(){intscore,temp,logic;chargrade;logic=1;while(logic) {scanf("%d",&score); if(score>=0&&score<=100)logic=0; n}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.7main(){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=%dn",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,%dn",ten_thousand,thousand,hundred,ten,indiv); printf("%d,%d,%d,%d,%dn",indiv,ten,hundred,thousand,ten_thousand); break; case4:printf("%d,%d,%d,%dn",thousand,hundred,ten,indiv); printf("%d,%d,%d,%dn",indiv,ten,hundred,thousand); break; case3:printf("%d,%d,%dn",hundred,ten,indiv); printf("%d,%d,%dn",indiv,ten,hundred); break; case2:printf("%d,%dn",ten,indiv); printf("%d,%dn",indiv,ten); break; case1:printf("%dn",indiv); printf("%dn",indiv); }}n5.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(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: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(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%dn",a,b,c,d);}n5.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)*(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.1main(){inta,b,num1,num2,temp;scanf("%d,%d",&num1,&num2);if(num1='a'&&c<='z'||c>='A'&&c<='Z')letters++; elseif(c=='')space++; elseif(c>='0'&&c<='9')digit++; elseother++; }printf("letters=%dnspace=%dndigit=%dnother=%dn",letters,space,digit,other);}6.3main(){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+...=%dn",sn);}n6.4main(){floatn,s=0,t=1;for(n=1;n<=20;n++) {t*=n; s+=t; }printf("s=%en",s);}6.5main(){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.2fn",s1+s2+s3);}6.6main(){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=%dn",n); }}6.7#defineM1000main(){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;i1)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(",%dn",k9); } }}main(){staticintk[10];inti,j,n,s;for(j=2;j<=1000;j++) {n=-1; s=j; for(i=1;i0) {x1=(x2+1)*2; x2=x1; day--; }printf("x1=%dn",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.2fn,xn1=%8.2fn",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.2fn",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; n}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 {x1=x0; fx1=fx0; } }while(fabs(fx0)>=1e-5);printf("x0=%6.2fn",x0);}6.14main(){inti,j,k;for(i=0;i<=3;i++) {for(j=0;j<=2-i;j++) printf(""); for(k=0;k<=2*i;k++) printf("*"); printf("n"); }for(i=0;i<=2;i++) {for(j=0;j<=i;j++) printf(""); for(k=0;k<=4-2*i;k++) printf("*"); printf("n"); }}6.15main(){chari,j,k;for(i='x';i<='z';i++) for(j='x';j<='z';j++) {if(i!=j) for(k='x';k<='z';k++) {if(i!=k&&j!=k) {if(i!='x'&&k!='x'&&k!='z') printf("na--%ctb--%ctc--%cn",i,j,k); } } }}第七章数组7.1#include#defineN101main(){inti,j,line,a[N];for(i=2;ia[j])min=j; temp=a[i]; a[i]=a[min]; a[min]=temp; }for(i=0;iend)a[10]=number;else {for(i=0;i<10;i++) n{if(a[i]>number) {temp1=a[i]; a[i]=number; for(j=i+1;j<11;j++) {temp2=a[j]; a[j]=temp1; temp1=temp2; } break; } } }for(i=0;i<11;i++) printf("%6d",a[i]);}7.5#defineN5main(){inta[N]={8,6,5,4,1},i,temp;for(i=0;in)) {i=i+2; j=j-1; } else {if(i<1)i=n; if(j>n)j=1; } if(a[i][j]==0)a[i][j]=k; else {i=i+2; j=j-1; a[i][j]=k; } }for(i=1;i<=n;i++) {for(j=1;j<=n;j++) printf("%3d",a[i][j]); printf("n"); }}7.8#defineN10#defineM10main(){inti,j,k,m,n,flag1,flag2,a[N][M],max,maxi,maxj;scanf("%d,%d",&n,&m);for(i=0;ia[k][maxj])flag1=0; if(flag1) {printf("na[%d][%d]=%dn",i,maxj,max); flag2=1; } }if(!flag2)printf("NOT");}7.9#include#defineNn15main(){inti,j,number,top,bott,min,loca,a[N],flag;charc;for(i=0;i<=N;i++) scanf("%d",&a[i]);flag=1;while(flag) {scanf("%d",&number); loca=0; top=0; bott=N-1; if((numbera[N-1])) loca=-1; while((loca==0)&&(top<=bott)) {min=(bott+top)/2; if(number==a[min]) {loca=min; printf("number=%d,loca=%dn",number,loca+1); } elseif(number