C语言程序设计(第3版张磊编著)参考答案

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

第三章二.1.#includestdio.hvoidmain(){floatweight=0;floatpay=0;printf(请输入邮件重量(单位:g):);scanf(%f,&weight);if(weight100){pay=100*0.05+(weight-100)*0.02;}else{pay=weight*0.05;}printf(\n需要支付:%f元,pay);}2.#includestdio.hvoidmain(){floatx=0;floaty=0;printf(请输入x的值:);scanf(%f,&x);if(x=0){y=x;}elseif(x0&&x1){y=2*x;}else{y=3*x*x-6*x+7;}printf(\ny的值为:%f,y);}3.#includestdio.hvoidmain(){inta=0,b=0,c=0;inttemp=0;printf(请以空格隔开输入3个整数\n);scanf(%d%d%d,&a,&b,&c);temp=a;if(tempb){a=b;b=temp;temp=a;}if(tempc){a=c;c=temp;temp=a;}if(bc){temp=b;b=c;c=temp;}printf(\n%d%d%d,a,b,c);}4.voidmain(){inta=0;printf(请输入一个整数:);scanf(%d,&a);if(a%3==0&&a%5==0){printf(%d该数既能被3整除又能被5整除,a);}else{printf(%d该数不能被3整除同时又能被5整除,a);}}5.voidmain(){inti=0;printf(请输入一个整数:);scanf(%d,&i);switch(i){case1:printf(星期一);break;case2:printf(星期二);break;case3:printf(星期三);break;case4:printf(星期四);break;case5:printf(星期五);break;case6:printf(星期六);break;case7:printf(星期天);break;default:printf(输入数据不合法\n);}}6.#includemath.hvoidmain(){floata=0,b=0,c=0;floatdt=0;printf(请以空格隔开输入abc的值:);scanf(%f%f%f,&a,&b,&c);dt=b*b-4*a*c;if(dt0){printf(\n无解);}elseif(dt==0){printf(\n有唯一解为:%f\n,-b/2*a);}else{printf(x1为:%fx2为:%f\n,(sqrt(dt))/2.0*a-b,(-(b+sqrt(dt))/2.0*a));}}7.voidmain(){charch;scanf(%c,&ch);if(ch='a'&&ch='z'||ch='A'&&ch='Z'){printf(这是字母:%c,ch);}elseif(ch='0'&&ch='9'){printf(这是数字:%c,ch);}else{printf(这是空白);}}8.voidmain(){intn=0,i=1;printf(输入n:);scanf(%d,&n);intsum=1;loop:sum=sum*i;if(in){i++;gotoloop;}printf(\n%d!=%d\n,n,sum);}9.voidmain(){intnum=0,temp=0,a=1;inti=0;//位数printf(输入一个整数:);scanf(%d,&num);temp=num;printf(\n逆序输出为:);do{i++;a=a*10;printf(%d,num%10);num=num/10;}while(num0);printf(\n位数:%d\n,i);printf(%d,a);printf(分别打印每一位数:);while(i0){a=a/10;printf(%d\t,temp/a);temp=temp-temp/a*a;i--;}}第四章一.选择题1~8:CCCAACCA二.程序分析题1.n%4!=02.1003.14.1三.1.voidmain(){intm=0,n=0,sum=1;inti=0;printf(请以空格输入mn);scanf(%d%d,&m,&n);for(i=1;i=m+n;i++){sum=i*sum;}printf(%d,sum);}2.voidmain(){inti=0;for(i=1;i=100;i++){if(i%7!=0)printf(%d\t,i);}}3.voidmain(){ints=0,n=0,i=0;while(s=1000){s=n+s;n++;}n--;s=s-n;printf(s的值为:%dn的值为:%d,s,n);}4.voidmain(){floata[20],temp=0;inti=0;for(i=0;i20;i++){scanf(%f,&a[i]);}temp=a[0];for(i=0;i20;i++){if(tempa[i])temp=a[i];}printf(最大值为:%f,temp);}5.voidmain(){charstr[100]={0};inti;printf(Pleaseinputastring:);gets(str);for(i=0;str[i]!=0;i++)if(str[i]!='a'&&str[i]!='A'&&str[i]!='f'&&str[i]!='5'&&str[i]!='#')printf(%c,str[i]);printf(\n);}6.图案一voidmain(){inti=4,j=0;for(i=0;i=4;i++)//控制输出的循环次数{for(j=0;j4-i;j++)//控制每行输出多少个空格{printf();}for(j=0;j2*i-1;j++)//控制每行输出多少个*printf(*);printf(\n);}for(i=3;i0;i--){for(j=4;ji;j--){printf();}for(j=0;j2*i-1;j++)printf(*);printf(\n);}}图案二:voidmain(){inti=0,n=5,j=0;for(i=5;i0;i--){for(j=0;j2*i-2;j++){printf();}for(j=5-i+1;j0;j--){printf(%d,j);printf();}printf(\n);}}7.voidmain(){inti=0,a,b,c;for(i=100;i1000;i++){a=i/100;b=i%100/10;c=i%10;if(a*a*a+b*b*b+c*c*c==i)printf(%d\t,i);}}8.voidmain(){doublea=100;inti=0;while(a1){a=a/2.0;i++;}printf(%d\n,i);}9.voidmain(){inta=1,b=2,c=5,sum=0;for(inti=0;i=100;i++){for(intj=0;j=50;j++){for(intk=0;k=20;k++){if(a*i+b*j+c*k==100){sum+=1;printf(%d个一分,%d个2分,%d个5分\n,i,j,k);}}}}printf(共%d种换法,sum);}10.#includestdio.h#includemath.hvoidmain(){doubleh1=100.0,h2;inti;h2=h1;for(i=1;i=10;i++){printf(第%d次落地反弹高度:\n,i);//第X次落地后反弹高度h2=h2/2;printf(%f\n,h2);printf(第%d次落地经过的路程\n,i);//第x次落地时经过的路程printf(%f\n,h1);h1=h1+(h2*2);}}11.main(){doublesum=0,n=1;inti;for(i=1;i=64;i++){sum=sum+n;n=2*n;}printf(%lf\n,sum/5000000);}12.voidmain(){//char*stateName[]={1站,2站,3站,4站,5站,6站,7站,8站,9站,A站,B站,C站,D站,E站,F站};charstateName[][15]={1站,2站,3站,4站,5站,6站,7站,8站,9站,A站,B站,C站,D站,E站,F站};inti,j,count=0;for(i=0;i15;i++)for(j=0;j15;j++)if(i!=j){count++;printf(序号%3d从%s到%s\n,count,stateName[i],stateName[j]);}printf(共%3d种票\n,count);}13.floatfun(intn){floatS=0,p=0;inti;for(i=1;i=n;i++){p+=i;S+=1/p;}returnS;}voidmain(){intn;floatS;printf(\n请输入N:);scanf(%d,&n);S=fun(n);printf(结果为%f\n,S);}第五章一.选择题:1.~3BDB二.程序分析题1.k=p2.Str[i]!=’\0’;str[i]=97;i++;3.34.b[j][i]=a[i][j]三.编程题1.#includestdio.hmain(){inta=1,b=1,c[20],i,j=0;for(i=0;i10;i++){a=a+b;if(a%2==0)c[j++]=a;b=a+b;if(b%2==0)c[j++]=b;}c[j]='\0';for(i=0;ij-1;i++)printf(%d\t,c[i]);}2.#includestdio.hvoidinverse(inta[],intn){inti,j,t;for(i=0,j=n-1;ij;++i,--j){t=a[i];a[i]=a[j];a[j]=t;}}voidmain(){inti,a[6]={1,5,7,13,15,19},n=6;inverse(a,n);for(i=0;in;i++)printf(%d,a[i]);}3.#includestdio.hvoidmain(){intn[6]={23,25,30,33,40,47},i,minflag;doubleaver=0,min,sum=0,t;for(i=0;i6;i++)sum+=n[i];aver=sum/6;min=aver-n[0];if(min=0)minflag=0;for(i=1;i6;i++){t=aver-n[i];if(t=0&&tmin){minflag=i;min=t;}}printf(%lf,%d\n,aver,minflag);}4.#includestdio.h#includestring.hvoidmain(){chars[80];inti,n,times=0;gets(s);n=strlen(s);for(i=0;in;i++)if(s[i]=='a')times+=1;printf(\na出现的次数为:%d\n,times);}5.#includestdio.h#includestring.hmain(){chara[5][81]={0};intcounter=0,i,j;printf(请输入5段英文。\n);for(i=0;i5;i++)gets(a[i]);for(i=0;i5;i++){for(j=0;jstrlen(a[i]);j++)if(a[i][j]!='')counter++;}flo

1 / 28
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功