1、C程序设计第三版课后题答案习题答案第一章1.5# include void main()printf(* * * * * * * * *n Very good!n* * * * * * * * *n);1.6(法一)#include main()int a,b,c,max;scanf(%d,%d,%d,&a,&b,&c);max=(ab)?a:b;max=(maxc)?max:c;printf(max=%dn,max);(法二)main()int a,b,c,max;scanf(%d,%d,%d,&a,&b,&c);if(ab&ac) max=a;else if (bc) max=b;else
2、 max=c;printf(max=%dn,max);(法三)# include void main()int max(int x,int y,int z);int a,b,c,d;scanf(%d,%d,%d,&a,&b,&c);d=max(a,b,c);printf(max=%dn,d);int max(int x,int y,int z)int A,B;if(xy) A=x;else A=y;if(zA) B=z;else B=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
3、)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()char c1=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()int c
4、1,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()int a,b,c;long int u,n;float x,y,z;char c1,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=%2d b=%2d c=%2dn,a,b,c);printf(x=%8.6f,y=%8.6f,z=%9.6fn,x,y,z)
5、;printf(x+y=%5.2f y+z=%5.2f z+x=%5.2fn,x+y,y+z,z+x);printf(u=%6ld n=%9ldn,u,n);printf(c1=%cor %d(ASCII)n,c1,c1);printf(c2=%cor %d(ASCII)n,c2,c2);4.5575 767.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,4553
6、207,d68765535,177777,ffff,-1COMPUTER, COM4.6a=3 b=7/x=8.5 y=71.82/c1=A c2=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()float pi,h,r,l,s,sq,sv,sz;pi=3.1415926;printf(input r,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
7、;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()float c,f;scanf(%f,&f);c=(5.0/9.0)*(f-32);printf(c=%5.2fn,c);4.10#includestdio.hmain()char c1,c2;scanf(%c,%c,&c1,&c2);putchar(c1);putchar(c2);printf(n);printf(%c%cn,c1,c2);第五章5.3(1)0 (2)1 (
8、3)1 (4)0 (5)15.4main()int a,b,c;scanf(%d,%d,%d,&a,&b,&c);if(ab) if(bc) printf(max=%dn,c); else printf(max=%dn,b);else if(ab)?a:b;max=(ctemp)?c:temp;printf(max=%d,max);5.5main()int x,y;scanf(%d,&x);if(x1)y=x;else if(x=0&score9999) place=5;else if(num999) place=4;else if(num99) place=3;else if(num9) p
9、lace=2;else place=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) case 5:pr
10、intf(%d,%d,%d,%d,%dn,ten_thousand,thousand,hundred,ten,indiv); printf(%d,%d,%d,%d,%dn,indiv,ten,hundred,thousand,ten_thousand); break; case 4:printf(%d,%d,%d,%dn,thousand,hundred,ten,indiv); printf(%d,%d,%d,%dn,indiv,ten,hundred,thousand); break; case 3:printf(%d,%d,%dn,hundred,ten,indiv); printf(%d
11、,%d,%dn,indiv,ten,hundred); break; case 2:printf(%d,%dn,ten,indiv); printf(%d,%dn,indiv,ten); break; case 1:printf(%dn,indiv); printf(%dn,indiv); 5.8main()long i;float bonus,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
12、*0.015;scanf(%ld,&i);if(i=1e5)bonus=i*0.1;else if(i=2e5)bonus=bon1+(i-100000)*0.075;else if(i=4e5)bonus=bon2+(i-200000)*0.05;else if(i=6e5)bonus=bon4+(i-400000)*0.03;else if(i10)branch=10;switch(branch)case 0:bonus=i*0.1;break;case 1:bonus=bon1+(i-100000)*0.075;break;case 2:case 3:bonus=bon2+(i-2000
13、00)*0.05;break;case 4:case 5:bonus=bon4+(i-400000)*0.03;break;case 6:case 7case 8:case 9:bonus=bon6+(i-600000)*0.015;break;case 10:bonus=bon10+(i-1000000)*0.01;printf(bonus=%10.2f,bonus);4.9main()int t,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
14、)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 %dn,a,b,c,d);5.10main()int h=10;float x,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(d11&d21&d31&d41)h=0;print
15、f(h=%d,h);第六章 循环控制6.1main()int a,b,num1,num2,temp;scanf(%d,%d,&num1,&num2);if(num1=a&c=A&c=0&c=9)digit+; else other+; printf(letters=%dnspace=%dndigit=%dnother=%dn,letters,space,digit,other);6.3main()int a,n,count=1,sn=0,tn=0;scanf(%d,%d,&a,&n);while(count=n) tn+=a; sn+=tn; a*=10; +count; printf(a+a
16、a+aaa+.=%dn,sn);6.4main()float n,s=0,t=1;for(n=1;n=20;n+) t*=n; s+=t; printf(s=%en,s);6.5main()int N1=100,N2=50,N3=10;float k;float s1=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()int i,j,k,n;for(n=100;n1000;n+) i=n/100; j=n/10-
17、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#define M 1000main()int k0,k1,k2,k3,k4,k5,k6,k7,k8,k9;int i,j,n,s;for(j=2;j=M;j+) n=0; s=j; for(i=1;i1)printf(%d,%d,k0,k1); if(n2)printf(,%d,k2); if(n3)printf(,%d,k3); if(n4)printf(,%d,k4); if(n5)printf(,%d,k5); if(n6)printf(,%d,k6
18、); if(n7)printf(,%d,k7); if(n8)printf(,%d,k8); if(n9)printf(,%dn,k9); main()static int k10;int i,j,n,s;for(j=2;j=1000;j+) n=-1; s=j; for(i=1;ij;i+) if(j%i)=0) n+; s=s-i; kn=i; if(s=0) printf(j=%dn,j); for(i=0;in;i+) printf(%d,ki); printf(%dn,kn); 6.8main()int n,t,number=20;float a=2;b=1;s=0;for(n=1;
19、n=number;n+) s=s+a/b; t=a,a=a+b,b=t; printf(s=%9.6fn,s);6.9main()float sn=100.0,hn=sn/2;int n;for(n=2;n0) x1=(x2+1)*2; x2=x1; day-; printf(x1=%dn,x1);6.11#includemath.hmain()float a,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
20、n,xn1=%8.2fn,a,xn1);6.12#includemath.hmain()float x,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#includemath.hmain()float x0,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
21、(fx1*fx20);do x0=(x1+x2)/2; fx0=x0*(2*x0-4)*x0+3)-6; if(fx0*fx1)=1e-5);printf(x0=%6.2fn,x0);6.14main()int i,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()char i,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 #define N 101main() int i,j,line,aN;for (i=2;iN;i+) ai=i;for (i=2;isqrt(N);i+) for (j=i+1;jN;j+) if(ai!=0 & aj!=0) if (aj%ai=0) a
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1