C语言程序设计苏小红课后习题参考答案高等教育出版社_第1页
C语言程序设计苏小红课后习题参考答案高等教育出版社_第2页
C语言程序设计苏小红课后习题参考答案高等教育出版社_第3页
C语言程序设计苏小红课后习题参考答案高等教育出版社_第4页
C语言程序设计苏小红课后习题参考答案高等教育出版社_第5页
免费预览已结束,剩余13页可下载查看

下载本文档

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

文档简介

1、2.2#include<stdio.h>main()floatx=2.5,y=2.5,z=2.5;printf("x=%fn",x);printf("y=%fn",y);printf("z=%fn",z);3.1(1)#include<stdio.h>main()inta=12,b=3;floatx=18.5,y=4.6;printf("%fn",(float)(a*b)/2);printf("%dn",(int)x%(int)y);3.1(2)#include<s

2、tdio.h>main().'intx=32,y=81,p,q;p=x+;q=-y;printf("%d%dn",p,q);printf("%d%dn",x,y);3.2#include<stdio.h>main()intx,b0,b1,b2,s;printf("Inputx:");scanf("%d",&x);b2=x/100;b1=(x-b2*100)/10;/或(x%100)/10;或x/10%10;b0=x%10;s=b0*100+b1*10+b2;printf("

3、;s=%dn",s);3.3#include<stdio.h>#include<math.h>main()floatrate=0.0225;floatn,capital,deposit;printf("Inputn,capital:");scanf("%f,%f",&n,&capital);deposit=capital*pow(1+rate,n);printf("deposit=%fn",deposit);3.4#include<stdio.h>#include<m

4、ath.h>main()floata,b,c;doublex,y;printf("Inputa,b,c:");scanf("%f%f%f",&a,&b,&c);x=(-b+sqrt(b*b-4*a*c)/(2*a);y=(-b-sqrt(b*b-4*a*c)/(2*a);printf("x=%f,y=%fn",x,y);习题44.1(1)#include<stdio.h>main()charc1='a',c2='b',c3='c'printf(&

5、quot;a%cb%cc%cn",c1,c2,c3);4.1(2)#include<stdio.h>main()inta=12,b=15;printf("a=%d%,b=%d%n”,a,b);一4.1(3)#include<stdio.h>main()inta,b;scanf("%2d%*2s%2d",&a,&b);printf("%d,%dn",a,b);4.2#include<stdio.h>main()longa,b;floatx,y;scanf("%d,%dn&qu

6、ot;,&a,&b);scanf("%f,%fn",&x,&y);printf("a=%d,b=%dn",a,b);printf("x=%f,b=%fn",x,y);5.1#include<stdio.h>main()floata;printf("Inputa:");scanf("%f",&a);if(a>=0)a=a;printf("a=%fn",a);elsea=-a;printf("a=%fn"

7、;,a);5.2#include<stdio.h>main()inta;printf("Inputa:");scanf("%d",&a);if(a%2=0)printf("a是偶数)elseprintf("a是奇数)5.3#include<stdio.h>#include<math.h>main()floata,b,c,s,area;printf("Inputa,b,c:");scanf("%f%f%f",&a,&b,&c);i

8、f(a+b>c&&a+c>b&&b+c>a)s=(a+b+c)/2;area=(float)sqrt(s*(s-a)*(s-b)*(s-c);printf("area=%fn",area);elseprintf("不是三角形");5.4#include<stdio.h>#include<math.h>main()floata,b,c,x,y;printf("Inputa,b,c:");scanf("%f,%f,%f",&a,&

9、b,&c);if(a=0)printf("该方程不是一元二次方程n");if(b*b-4*a*c>0)x=(-b+sqrt(b*b-4*a*c)/(2*a);y=(-b-sqrt(b*b-4*a*c)/(2*a);printf("x=%f,y=%fn",x,y);elseif(b*b-4*a*c=0)x=-b/(2*a);y=-b/(2*a);printf("x=%f,y=%fn",x,y);elseprintf("该方程无实根n");5.5#include<stdio.h>main()i

10、ntyearflag;printf("Inputayear:");scanf("%d”,&year);if(year%4=0&&year%400!=0|year%400=0)flag=1;elseflag=0;if(flag=1)printf("%disaleapyear!n",year);elseprintf("%disnotaleapyear!n",year);5.6#include<stdio.h>main()intyearflag;printf("Inputayear:&

11、quot;);scanf("%d”,&year);flag=year%400=0|year%4=0&&year%100!=0?1:0;if(flag=1&&flag!=0)printf("%disaleapyear!n",year);elseprintf("%disnotaleapyear!n",year);5.7#include<stdio.h>main()charch;printf("Inputch:");scanf("%c",&ch);if

12、(ch>='a'&&ch<='z')ch=getchar();ch=ch-32;printf("%c,%dn",ch,ch);elseif(ch>='A'&&ch<='Z')ch=getchar();ch=ch+32;printf("%c,%dn",ch,ch);elseprintf("%c",ch);5.8#include<stdio.h>main()charch;printf("Inputc

13、h:");scanf("%c",&ch);if(ch>=48&&ch<=57)printf("ch是数字字符n");elseif(ch>=65&&ch<=90)printf("ch是大写字母n");elseif(ch>=97&&ch<=122)printf("ch是小写字母n");elseif(ch=32)printf("ch是空格n");elseprintf("ch是其他字符n&qu

14、ot;);5.9#include<stdio.h>main()intscore,grade;printf("Inputscore:");scanf("%d”,&score);grade=score/10;if(score<0|score>100)printf("Inputerrorn");if(score>=90&&score<=100)printf("%d-An",score);elseif(score>=80&&score<90)pr

15、intf("%d-Bn",score);elseif(score>=70&&score<80)printf("%d-Cn",score);elseif(score>=60&&score<70)printf("%d-Dn",score);elseif(score>=0&&score<60)printf("%d-En",score);5.10#include<stdio.h>main()intyear,month;print

16、f("Inputyear,month:");scanf("%d,%d",&year,&month);if(month>12|month<=0)printf("errormonthn");-i:I'l1-elseswitch(year,month)case12:I.case10:case8:case7:case5:case3:case I:printf("31天n");break;case II:case9:case6:case4:printf("30天n");

17、break;case2:if(year%4=0&&year!=0|year%400=0)printf("29天n");elseprintf("28天n");break;default:printf("Inputerrorn");6.1(1)#include<stdio.h>main()inti,j,k;charspace=''for(i=1;i<=4;i+)for(j=1;j<=i;j+)printf("%c",space);for(k=1;k<=6;k

18、+)printf("*");printf("n");6.1(2)#include<stdio.h>main()intk=4,n;for(n=0;n<k;n+)if(n%2=0)continue;k-;printf("k=%dn,n=%dn",k,n);6.1(3)#include<stdio.h>main()intk=4,n;for(n=0;n<k;n+)if(n%2=0)break;k-;printf("k=%d,n=%dn",k,n);6.2(1)#include<st

19、dio.h>main()inti,sum=0;for(i=1;i<=101;i+)sum=sum+i;printf("sum=%dn",sum);6.2(2)#include<stdio.h>main()longi;j1-longterm,sum=0;for(i=1;i<=101;i=i+2)term=i*(i+1)*(i+2);sum=sum+term;printf("sum=%ldn”,sum);6.2(4)#include<stdio.h>#include<math.h>main()intn=1;floa

20、tterm=1.0,sign=1,sum=0;while(term<=-1e-4|term>=1e-4)term=1.0/sign;sum=sum+term;sign=sign+n;n+;printf("sum=%fn",sum);6.2 (5)#include<stdio.h>#include<math.h>main()intn=1,count=1;floatx;doublesum,term;printf("Inputx:");scanf("%f",&x);sum=x;term=x;dot

21、erm=-term*x*x/(n+1)*(n+2);sum=sum+term;n=n+2;count+;while(fabs(term)>=1e-5);printf("sin(x)=%f,count=%dn",sum,count);6.3 #include<stdio.h>main()-.IlI''l1-;intx=1,find=0;while(!find)if(x%2=1&&x%3=2&&x%5=4&&x%6=5&&x%7=0)printf("x=%dn"

22、,x);find=1;x+;/*intx,find=0;for(x=1;!find;x+)if(x%2=1&&x%3=2&&x%5=4&&x%6=5&&x%7=0)printf("x=%dn",x);find=1;*/6.4 #include<stdio.h>main()inti,n;longp=1,m=1;printf("Inputn:");scanf("%d",&n);for(i=1;i<=n;i+)p=i*i;m=i*i*i;printf(

23、"p=%d,m=%dn",i,p,i,m);6.5 #include<stdio.h>main()floatc,f;for(c=-40;c<=110;c=c+10)f=9/5*c+32;printf("f=%fn",f);6.6 #include<stdio.h>#include<math.h>main()Iintn;doublec=0.01875,x;dox=x*pow(1+c,12)-1000;n+;while(x>0);printf("x=%dn",x);6.7 #include&

24、lt;stdio.h>main()intn=0;floata=100.0,c;printf("Inputc:");scanf("%f",&c);doa=a*(1+c);n+;while(a<=200);printf("n=%dn",n);6.8 #include<stdio.h>#include<math.h>main()intn=1,count=1;doublesum=1,term=1;while(fabs(term)>=1e-5)term=pow(-1,count)*(1.0/(n

25、+2);sum=sum+term;n=n+2;count+;r-,、工、,圮、产Isum=4*sum;-1"I'l,-!printf("sum=%f,count=%dn",sum,count);6.9 #include<stdio.h>#include<math.h>main()intn=1,count=1;doublesum=1,term=1;while(fabs(term)>=1e-5)term=term*(1.0/n);sum=sum+term;n+;count+;printf("sum=%f,count=%d

26、n",sum,count);6.10 #include<stdio.h>#include<math.h>main()intx;for(x=100;x<=999;x+)if(x=pow(x/100,3)+pow(x/10%10,3)+pow(x%10,3)printf("x=%dn",x);6.11 #include<stdio.h>main()inti=0,n;longsum=0,term=1;printf("Inputn:");scanf("%d",&n);doi+;ter

27、m=term*i;sum=sum+term;while(sum<n);printf("%dn",i-1);r-'葭%XXs'/*I6.12 #include<stdio.h>一一、.l1"I'l,-'">main()inti,n,m,count=0,sum=0;printf("Inputn:");scanf("%d",&n);for(i=1;i<=n;i+)Iprintf("Inputm:");scanf("%d&q

28、uot;,&m);if(m>0)sum=sum+m;count+;elsebreak;printf("sum=%d,count=%dn",sum,count);)6.13 #include<stdio.h>main()(inti,n,m,count=0,sum=0;printf("Inputn:");scanf("%d",&n);for(i=1;i<=n;i+)(printf("Inputm:");scanf("%d",&m);if(m>0|

29、m<0)(sum=sum+m;count+;)else(break;)printf("sum=%d,count=%dn",sum,count);)r-yV立JX.J!I)6.14 #include<stdio.h>"j''i1-:、main()(intx,y,z;for(x=0;x<=17;x+)for(y=0;y<=25;y+)3*x+2*y+z=50;z=30-x-y;if(3*x+2*y+z=50&&x+y+z=30)printf("x=%d,y=%d,z=%dn",x,y,z

30、);)6.15 #include<stdio.h>main()intx,y;for(x=0;x<=98;x+)y=98-x;2*x+4*y=386;if(x+y=98&&2*x+4*y=386)(printf("x=%d,y=%dn",x,y);)6.16 #include<stdio.h>main()(intx,y,z;for(x=0;x<=20;x+)(for(y=0;y<=33;y+)(3*y+5*x+z/3.0=100;z=100-x-y;if(5*x+3*y+z/3.0=100&&z+x+y

31、=100)(printf("x=%d,y=%d,z=%dn",x,y,z);)r-y5立b"rXX/!I)-i''i1);)6.17#include<stdio.h>main()intx,y,z;for(x=1;x<=9;x+)(for(y=1;y<=17;y+)(10*x+5*y+z=100;z=50-x-y;if(10*x+5*y+z=100&&x+y+z=50&&z>0)(printf("x=%d,y=%d,z=%dn",x,y,z);)7.1 #include

32、<stdio.h>intSquare(inti)returni*i;intmain()inti=0;i=Square(i);for(;i<3;i+)staticinti=1;i+=Square(i);printf("%d,",i);printf("%dn",i);return0;7.2 #include<stdio.h>inthour,minute,second;voidupdate()second+;if(second=60)''lIsecond=0;minute+;if(minute=60)minute=

33、0;hour+;if(hour=24)hour=0;voiddisplay。printf("%d,%d,%dn",hour,minute,second);voiddelay()intt;for(t=0;t<100000000;t+);intmain()inti;voidupdaye(),display(),delay();for(i=0;i<1000000;i+)update();display。;delay();return0;7.3 #include<stdio.h>intGetMax(inta,intb);intmain()intx,y,max

34、;printf("Inputx,y:");scanf("%d,%d",&x,&y);max=GetMax(x,y);printf("max=%dn",max);return0;'l,-intGetMax(intm,intn)if(m>=n)returnm;elseI.returnn;7.4 #include<stdio.h>intLCM(intn,intm);intmain()inta,b;printf("Inputa,b:");scanf("%d,%d"

35、;,&a,&b);printf("%dn",LCM(a,b);return0;intLCM(intn,intm)intx;intfind=0;for(x=1;!find;x+)if(x%n=0&&x%m=0)find=1;returnx-1;7.5 #include<stdio.h>longFact(intn);intmain()intm,a;printf("Inputm:");scanf("%d",&m);for(a=1;a<=m;a+)printf("%d!=%ldn",a,Fact(a);return0;i1,"l1IlongFact(intn)inti;longresult=1;for(i=2;i<=n;i+)Iresult*=i;returnresult;7.6#include<stdio.h>longFact(intn);intmain(

温馨提示

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

评论

0/150

提交评论