《《C语言程序设计案例教程》(第2版)习题答案 北邮.docx》由会员分享,可在线阅读,更多相关《《C语言程序设计案例教程》(第2版)习题答案 北邮.docx(13页珍藏版)》请在第一文库网上搜索。
1、模块1习题答案一、选择题1.D2.A3.B4.C5.C6.C二、模仿编程1 .参考程序:#inc1udestdio.hmain()(f1oatarea,r=2.5,pi=3.1415926;area=pi*r*r;Printf(area=%fn,area);)2 .略。模块2习题答案一、选择题1.B2.A3.D4.A5.D6.A7.B8.A9.C10.D二、编程题1 .参考程序:#inc1udestdio.hmain()(f1oatarea,szr=2.5,pi=3.1415926;area=pi*r*r;s=2*pi*r;printf(area=%ns=%fn,area,s);)2 .参考程
2、序:#inc1ude#inc1udemain()(inta;intb;intc,d;a=1500zb=350;c=ab;商数d=a%b;余数Printf(商数为:%dn,c);Printf(余数为:dn,d);return0;)3 .参考程序:#inc1ude#inc1udemain()(f1oata,bzx,z;a=2;b=3;x=3.5;y=2.6;z=(a+b)2+sqrt(a)-(y-x);printf(%f,z);)4 .略。模块3习题答案一、选择题1 .A2.C3.B4.D5.A6.C7.B8.D9.B10.D二、编程题1参考程序:#inc1udeintmain()(inthour
3、s;f1oatwage;f1oatsa1ary;SCanf(%d%f,&hours,&wage);sa1ary=hours*wage*(1-0.1);printf(thesa1aryshou1dbe%fn,sa1ary);)2 .参考程序:#inc1udeintmain()(f1oatf,c;Primf(输入华氏温度:n);scanf(%ff);c=5*(f-32)9;Printf(对应的摄氏温度是:%5.2fnzc);)3 .参考程序:#inc1ude#inc1udeintmain()(intnum,a,bzc;输入三位数Printf(请输入一个三位整数:n”);SCanf(%d,&num)
4、;输出各位数a=num100;b=(num-a*100)10;c=(num-a*100-b*10);Printf(千位:%d,十位:%d,个位:%dn,a,b,c);return0;)模块4习题答案一、选择题1.B2.B3.D4.C5.A6.C7.C8.C9.A10.A二、编程题1参考程序:#inc1udeintmain()(charc;c=getchar();读入一个字符,存储在c中。if(c=,ac=,ec=,i,c=,oc=uc=,Ac=,Ec=,c=O,c=U,)/判断是元音。printf(,%c是元音字母n,c);输出结果。e1se不是元音。printf(,%c不是元音字母nQ;输出
5、结果。return0;)2 .参考程序:inc1udeintmain()intnum;Printfr请输入整数:);scanf(%d,z&num);if(num%2=0)printf(,n%d是偶数,num);e1seprintf(,n%d是奇数,num);return0;)3 .答案略。4 .参考程序:#inc1udeintmain()(intc,s;f1oatp,w,d,f;scanf(%f%f%d,&p,&w,&s);if(s=3000)c=12;e1sec=s250;switch(c)(case0:d=0;break;case1:d=2;break;case2:d=5;break;ca
6、se3:d=8;break;case4:d=10;break;case5:d=15;break;)f=p*w*s*(1-d100);printf(freight=%15.4fnzf);)5 .参考程序:#inc1ude#inc1udemain()(intYear,Month,Day;coutYearMonth;whi1e(Month12)(CoUtYearMonth;)Switch(Month)(case4:case6:case 8:case 9:case11:Day=30;break;case2:if(Year%400=0Year%4=0&Year%100!=0)Day=29;e1seDay
7、=28;break;defau1t:Day=31;)COUtv”该月天数为:Day天;)6.答案略。模块5习题答案一、选择题1.D2.C3.D4.C5.C6.B7.B8.D9.A10.A二、编程题1 .参考程序:inc1udemain()(inti=1,sum=O,k=1;for(i=1;i=101;i=i+2)(if(k%2=1)sum=sum+i;e1sesum=sum-i;k+;)Printf(,求和为:%d”,SUm);)2 .参考程序:#inc1ude#inc1udemain()(inti,t;intcount=0;intbf1ag=O;*break标记*/for(i=100;i=2
8、00;i+)(bf1ag=O;for(t=2;t=sqrt(i);t+)if(i%t=O)bf1ag=1;break;)if(!bf1ag)(printf(%4dzi);+count;if(count%10=0)(count=0;printf(n);)printf(n);)3 .答案略。4 .答案略。5 .参考程序如下:#inc1ude#inc1udeintmain()(inta,b,c;intcount=0;Printf(共有以下借法:n);for(a=1;a=5;a+)/a,b,c,分别从一号书借到五号书(for(b=1;b=5;b+)(for(c=1;c=5;c+)(if(O!=(a-b
9、)*(b-c)*(c-a)(count+;printf(,%d:%d%d%dtzcount,a,b,c);if(0=count%3)(printf(n);)returnO;)6 .参考程序:#inc1udeinta25;intmain()(a0=a1=1;for(inti=2;i25;i+)ai=ai-1+ai-2;for(inti=0;i25;i+)(printf(%3d,zai);if(i+1)%5=0)printf(n);)printf(n);return0;)模块6习题答案一、选择题1.B2.A3.D4.B5.B6.D7.C8.A二、编程题(略)1 .参考程序:#inc1udeintm
10、ain()(doub1enum10;inti;doub1esum=O;Printf(”请连续输入十个实数:n“);for(i=0;i10;i+)(scanf(%1f,8tumi);)for(i=0;i10;i+)(sum+=numi;Printf(第d个实数的和为名1fr,i+1,sum);)return0;2 .参考程序:#inc1ude#inc1udemain()(chara100;iti,j;gets(a);i=0;j=str1en(a)-1;whi1e(i=j)printf(YES.n);e1seprintf(NO.n);)3 .参考程序:#inc1udevoiddec2bin(int
11、n)(if(n1)dec2bin(n2);printf(%dzn%2);)intmain()(intx;scanf(%d,x);dec2bin(x);printf(,n);return0;)4 .答案略。5 .答案略。模块7习题答案一、选择题1.D2.C3.B4.C5.C6.A7.D8.A9.ACD10.C二、编程题1 .参考程序:inc1udeintis_even(intn)(return!(n&Ox1);)intmain()(intn;scanf(%dn);if(is_even(n)printf(偶数n);e1sePrintf(”奇数n);return0;)2 .参考程序:#inc1ude
12、voidfun(int*a,intIzint*bzint*n)(inti;*n=0;for(i=0;iI;i+)if(ai%2=1)b(*n)+=ai;)intmain()(inta10=1,2,3,4,5,6,7,8,9,19;intb10;intn=0zi;fun(a,10,b,&n);for(i=0;in;i+)printf(%d,bi);return0;)3 .参考答案:#inc1udedoub1emu1(intn)(inti;doub1es=1.0;for(i=1;i=n;i+)returns;)intmain()(printf(c=6!+10!+22!=%gn,mu1(6)+mu1(10)+mu1(22);)