1C语言习题答案第1章1.选择题(1)C(2)B(3)A2.填空题(1)main(2)main(3)有穷性、确定性、有零个或多个输入、有一个或多个输出、有效性(4)顺序、分支和循环(5)自顶向下,逐步细化、模块化设计、结构化编码第2章1.选择题123456789012345ACCDADCABABBDDB2、填空题(1)数字、字母、下划线(2)0(3)4、8(4)(a+b)*c/(a-b)(5)-60(6)-16(7)9(8)6、4、2(9)0(10)10、6(11)5.500000(12)12、4(13)0(14)16(15)6.63、编程题(1)main(){printf(int:%5d\nfloat:%5d\nchar:%5d\ndouble:%5d\nlong:%5d\n,sizeof(int),sizeof(float),sizeof(char),sizeof(double),sizeof(long));}(2)#defineRAT1.60934main(){floatk=0.0;printf(inputthekm:);scanf(%f,&k);printf(\nmile:%f,k*RAT);}第3章1.选择题21234567890(1)~(10):DDCDDDCDCC2.解析题(1)x=170,x=ㄩㄩㄩ170,x=ㄩㄩㄩ252,x=ㄩㄩㄩㄩaa,x=ㄩㄩㄩ170x=170,x=170ㄩㄩㄩ,x=ㄩㄩㄩ170,x=%6da=513.789185,a=ㄩㄩ513.79,a=513.78918457,a=513.78918457(2)a=3ㄩb=7x=8.5ㄩy=71.82c1=Aㄩc2=a3.编程题(1)main(){intx,y;scanf(%d%d,&x,&y);printf(商数=%d,余数=%d,x/y,x%y);system(pause);}(2)main(){doublex,y,z,avg;scanf(%lf%lf%lf,&x,&y,&z);avg=(x+y+z)/3;printf(%.1lf,avg,avg);system(pause);}第4章1.选择题(1)~(10)CCAADCCABD2.填空题(1)ch='A'&&ch='Z'ch=ch-32(2)x=10&&x2x=2&&x-1y=-1;(3)a+bc&&a+cb&&b+caa==b&&a==ca==b||a==c||b==c(4)mark/10case9(5)x0c=x/10y!=-23.编程题(1)#includestdio.hmain(){intx;printf(pleaseinputanumber:);scanf(%d,&x);if(x%2==0)printf(xisaevennumber);elseprintf(xisaoddnumber);}3(2)#includestdio.hmain(){intx,y;printf(pleaseinputanumber£º);scanf(%d,&x);if(x=-5)printf(thenumberiserror);elseif(x0){y=x;printf(%d,y);}elseif(x==0){y=x-1;printf(%d,y);}elseif(x10){y=x+1;printf(%d,y);}elseprintf(thenumberiserror);}(3)#includestdio.hmain(){inta,m;printf(pleaseinputanumber:);scanf(%d,&a);switch(a/10){case0:case1:case2:m=1;break;case3:m=2;break;case4:m=3;break;case5:m=4;break;default:m=5;break;}printf(%d,m);}(4)#includestdio.hmain(){floatprice,tax;printf(pleaseinputthepriceofproduct:);scanf(%f,&price);if(price10000)tax=price*0.05;elseif(price5000)tax=price*0.03;elseif(price1000)tax=price*0.02;elsetax=0;printf(%f,tax);}(5)#includestdio.hmain()4{floatscore;printf(pleaseinputthescoreofstudent:);scanf(%f,&score);if(score=85)printf(VERYGOOD);elseif(score=60)printf(GOOD);elseprintf(BAD);}第5章1.选择题(1)d(2)c(3)a(4)d(5)a(6)d(7)d(8)b(9)d(10)b(11)c(12)a(13)d(14)d(15)c2.填空题(1)fahrcelsius=5.0/9*(fahr-32)fahr+=step(2)5、4、6(3)3*i-2(4)y--z*=x(5)8ㄩ5ㄩ2(6)j++i%j==0j=i(7)sumk&&jksum==kj-2(8)s=0p=1j=i3.改错题(1)第一处改正:For改为for第二处改正:ave=sum/4改为ave=sum/4.0(2)第一处改正:j=9第二处改正:m=100*i+10*j+k(3)第一处改正:temp=1第二处改正:temp=eps第三处改正:r*2(4)第一处改正:&n第二处改正:====(5)第一处改正:t=m/10改为t=m%10第二处改正:t=0改为t==0第三处改正:m=m%10改为m=/104.编程题(1)分式累加和。#includemath.hmain(){ints;floatn,t,sum;t=1;sum=0;n=1;s=1.0;while(n=100){sum=sum+t;n=n+1;s=-s;t=s/n;}5printf(sum=%10.6f\n,sum);}(2)1~10的阶乘#includestdio.hmain(){inti;longintn=1;for(i=1;i=10;i++){n=n*i;printf(i%5==0?%2d!=%-10ld\n:%2d!=%-10ld,i,n);}}(3)由*组成的等腰三角形#includestdio.hmain(){intn,i,j;scanf(%d,&n);for(i=1;i=n;i++){for(j=1;j=n+i-1;j++)if(j=n-i)printf();elseprintf(*);printf(\n);}}(4)完数main(){intm,s,i;for(m=2;m1000;m++){s=0;for(i=1;im;i++)if((m%i)==0)s=s+i;if(s==m){printf(%ditsfactorsare,m);for(i=1;im;i++)if(m%i==0)printf(%d,,i);printf(\n);}}}(5)打靶#includestdio.hmain()6{inth10,h7,h5;for(h10=0;h10=8;h10++)for(h7=0;h7=8;h7++)for(h5=0;h5=8;h5++)if(h10+h7+h5==8&&h10*10+h7*7+h5*5==53)printf(h10=%d\th7=%d\th5=%d\n,h10,h7,h5);}(6)猴子吃桃子问题。采取逆向思维的方法,从后往前推断。main(){intday,x1,x2;day=9;x2=1;while(day0){x1=(x2+1)*2;/*第一天的桃子数是第2天桃子数加1后的2倍*/x2=x1;day--;}printf(thetotalis%d\n,x1);}(7)牛顿迭代法#includemath.hmain(){floatx0,x1,x2,fx0,fx1,fx2;do{printf(Enterx1&x2:);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{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(x=%6.2f\n,x0);}7(8)利用辗除法,直到b为0为止main(){intp,r,n,m,temp;printf(inputtwointegern,m:);scanf(%d,%d,&n,&m);if(nm){temp=n;n=m;m=temp;}p=n*m;while(m!=0){r=n%m;n=m;m=r;}printf(greatestcommondivisoris:%d\n,n);//最大公约数printf(leasecommonmultipleis:%d\n,p/n);//最小公倍数}第6章1.选择题CCDCCAADBD2.写出程序的运行结果(1)sum=6(2)Max=9,row=1,col=0(3)0102103210(4)-53、填空(1)i=j=k=0i4&&j==5j5&&i==4(2)i=1&&j-1(3)n0n%10+0x30或n%10+'0's[j]='\0'(4)s[i]、s[0],s[1]、str,s[1]、s[2],str4.改错题(1)第一处改正:n=strlen(str)第二处改正:str[j]==''第三处改正:str[j]!=''(2)第一处改正:ch=str[i]第二处改正:(j=0)&&(chstr[j])8(3)第一处改正:s1[i]!='\0'第二处改正:j=0;(4)第一处改正:cnt=0第二处改正:if(!(m%i))第三处改正:icnt5.编程题(1)#includestdlib.hmain(){inta[12],count=0,i;randomize();for(i=0;i12;i++)a[i]=5+random(11);for(i=0;i12;i++){printf(%5d,a[i]);count++;if(count%4==0)printf(\n);}getch();}(2)main(){inta[5]={2,23,43,77,33};intmax,min,i;longsum=0L;intcount=0;floatav;max=min=a[0];for(i=0;i5;i++){sum+=a[i];if(a[i]max)max=a[i];if(a[i]min)min=a[i];}av=(float)sum/5;for(i=0;i5;i++)if(a[i]av)count++;printf(max:%d\nmin:%d\nav:%f\ncount:%d,max,min,av,count);getch();}9(3)main(){intscore[30];/*={61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,85,89,90,33,44,55,91,92,93};不必赋初值,这是为调试程序方便*/inti,count90=0,count80=0,count70=0,count60=0;for(i=0;i30;i++){printf(intputNO.%dstudent'sscore:,i+1);scanf(%d,score+i);}for(i=0;i30;i++){if(score[i]=90){count9