c程序设计答案11496new_第1页
c程序设计答案11496new_第2页
c程序设计答案11496new_第3页
c程序设计答案11496new_第4页
c程序设计答案11496new_第5页
已阅读5页,还剩49页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、习题答案第一章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 max=c;printf

2、(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)16(4)(-617)1

3、0=(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 c1,c2;c1=97;c2

4、=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);printf(x+y=%

5、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,4553207,d68765535

6、,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;printf(l=%6.

7、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 (3)1 (4)0 (5)1

8、5.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) place=2;else p

9、lace=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:printf(%d,%d,%d

10、,%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,%d,%dn,indiv

11、,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*0.015;scanf(

12、%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-200000)*0.05;brea

13、k;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)t=b;b=c;c=t;

14、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;printf(h=%d,h);第六章

15、 循环控制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+aa+aaa+.=%dn,s

16、n);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-i*10; k=n%10;

17、 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); if(n7)prin

18、tf(,%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;n=number;n+)

19、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.2fn,xn1=%8.2fn,

20、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(fx1*fx20);do

21、 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=

22、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) aj=0; printf(n);for (i=2,line=0;iN;i+) if(

23、ai!=0) printf(%5d,ai); line+; if(line=10) printf(n); line=0; 7.2#define N 10main()int i,j,min,temp,aN;for(i=0;iN;i+) scanf(%d,&ai);for(i=0;iN-1;i+) min=i; for(j=i+1;jaj)min=j; temp=ai; ai=amin; amin=temp; for(i=0;iN;i+) printf(%5d,ai);7.3main()float a33,sum;int i,j;for(i=0;i3;i+) for(j=0;j3;j+) scan

24、f(%f,&sum); aij=sum; for(i=0;iend) a10=number;else for(i=0;inumber) temp1=ai; ai=number; for(j=i+1;j11;j+) temp2=aj; aj=temp1; temp1=temp2; break; for(i=0;i11;i+) printf(%6d,ai);7.5#define N 5main()int aN=8,6,5,4,1,i,temp;for(i=0;iN/2;i+) temp=ai; ai=aN-i-1; aN-i-1=temp; for(i=0;iN;i+) printf(%4d,ai

25、);7.6#define N 11main()int i,j,aNN;for(i=1;iN;i+) aii=1; ai1=1; for(i=3;iN;i+) for(j=2;ji;j+) aij=ai-1j-1+ai-1j;for(i=1;iN;i+) for(j=1;j=i;j+) printf(%6d,aij); printf(n); 7.7main()int a1616,i,j,k,p,m,n;p=1;while(p=1) scanf(%d,&n); if(n!=0)&(n=15)&(n%2!=0)p=0; for(i=1;i=n;i+) for(j=1;j=n;j+) aij=0;j=

26、n/2+1;a1j=1;for(k=2;k=n*n;k+) i=i-1; j=j+1; if(in) i=i+2; j=j-1; else if(in)j=1; if(aij=0)aij=k; else i=i+2; j=j-1; aij=k; for(i=1;i=n;i+) for(j=1;j=n;j+) printf(%3d,aij); printf(n); 7.8#define N 10#define M 10main()int i,j,k,m,n,flag1,flag2,aNM,max,maxi,maxj;scanf(%d,%d,&n,&m);for(i=0;in;i+) for(j=

27、0;jm;j+) scanf(%d,&aij);flag2=0;for(i=0;in;i+) max=ai0; for(j=0;jm;j+) if(maxaij) max=aij; maxj=j; for(k=0,flag1=1;kakmaxj)flag1=0; if(flag1) printf(na%d%d=%dn,i,maxj,max); flag2=1; if(!flag2) printf(NOT);7.9#include#define N 15main()int i,j,number,top,bott,min,loca,aN,flag;char c;for(i=0;i=N;i+) sc

28、anf(%d,&ai);flag=1;while(flag) scanf(%d,&number); loca=0; top=0; bott=N-1; if(numberaN-1) loca=-1; while(loca=0)&(top=bott) min=(bott+top)/2; if(number=amin) loca=min; printf(number=%d,loca=%dn,number,loca+1); else if(numberamin) bott=min-1; else top=min+1; if(loca=0|loca=-1) printf(%d not in tablen

29、,number); printf(continue Y/N or y/nn); c=getchar(); if(c=N|c=n)flag=0; 7.10main()int i,j,uppn,lown,dign,span,othn;char text380;uppn=lown=dign=span=othn=0;for(i=0;i3;i+) gets(texti); for(j=0;j=A&textij=a&textij=0&textij=9) dign+; else if(textij= ) span+; else othn+; for(i=0;i3;i+) printf(%sn,texti);

30、printf(uppn=%dn,uppn);printf(lown=%dn,lown);printf(dign=%dn,dign);printf(span=%dn,span);printf(othn=%dn,othn);7.11main()static char a5=*,*,*,*,*;int i,j,k;char space= ;for(i=0;i=5;i+) printf(n); for(j=1;j=3*i;j+) printf(%1c,space); for(k=0;k=5;k+) printf(%3c,ak); 7.12#includemain()int i,n;char ch80,

31、tran80;gets(ch);i=0;while(chi!=0) if(chi=A)&(chi=a)&(chi=z) trani=26+96-chi+1+96; else trani=chi; i+; n=i;for(i=0;in;i+) putchar(trani);7.13main()char s180,s240;int i=0,j=0;scanf(%s,s1);scanf(%s,s2);while(s1i!=0)i+;while(s2j!=0)s1i+=s2j+;s1i=0;printf(s=%sn,s1);7.14#includemain()int i,resu;char s1100

32、,s2100;gets(s1);gets(s2);i=0;while(s1i=s2i)&(s1i!=0)i+;if(s1i=0&s2i=0)resu=0;else resu=s1i-s2i;printf(s1=%s,s2=%s,resu=%dn,s1,s2,resu);7.15#includestdio.hmain()char from80,to80;int i;scanf(%s,from);for(i=0;iv)t=u;u=v;v=t;a=u;b=v;while(r=b%a)!=0) b=a;a=r;return(a);lcd(u,v,h)int u,v,h;return(u*v/h);ma

33、in()int u,v,h,l;scanf(%d,%d,&u,&v);h=hcf(u,v);printf(H.C.F=%dn,h);l=lcd(u,v,h);printf(L.C.D=%dn,l);8.2#includemath.hfloat x1,x2,disc,p,q;greater_than_zero(a,b)float a,b;x1=(-b+sqrt(disc)/(2*a);x2=(-b-sqrt(disc)/(2*a);equal_to_zero(a,b)flaot a,b;x1=x2=-b/(2*a);smaller_than_zero(a,b)float a,b;p=-b/(2*

34、a);q=sqrt(-disc)/(2*a);main()float a,b,c;scanf(%f,%f,%f,&a,&b,&c);disc=b*b-4*a*c;if(fabs(disc)0) greater_than_zero(a,b); printf(x1=%5.2ftx2=%5.2fn,x1,x2); else smaller_than_zero(a,b); printf(x1=%5.2f+%5.2fitx2=%5.2f-%5.2fin,p,q,p,q); 8.3main()int number;scanf(%d,&number);if(prime(number) printf(yes);else printf(no);int p

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论