《精通C程序设计教程》部分习题答案2005年4月说明:本答案没有仔细校对,如果发现错误,请自行更正;蓝色的程序经过了上机验证.第二章C源程序基本语法与顺序结构简单程序设计2.10ABEFG2.11ACFHJMNPQRT2.12(1)3(2)0(3)0(4)1(5)–1(6)160(7)4(8)4(9)8(10)8(11)1(12)2(13)1(14)0(15)6(16)0(17)29(18)3(19)1(20)12.132.52.143.52.15(1)4311(2)4222(3)32122.16(1)00(2)100100(3)22(4)11(5)2020(6)1141152.17(1)–y*y*y*y*(a*a-b*b)(2)sin(y/sqrt(x*x+y*y))(3)sqrt(1+3.14/2*tan(48.5/180*3.14))(4)a/x/(a/(a+y)+b/(a+b/(a+b/z)))(5)p*q*(r+1)*(r+1)/((r+1)*(r+1)-1)(6)fabs(3-exp(-x)*log(fabs(1+x)))2.20#include“stdio.h”voidmain(){longa;intb;doublex;floaty;scanf(“%ld%d%lf%f”,&a,&b,&x,&y);printf(“a=%lo,b=%.0f,x=%0G,y=%Lg\n”,a,(float)b,x,(longdouble)y);}2.22#include“stdio.h”#include“math.h”voidmain(){doublea,b,c,p,s;printf(“Inputabc:”);scanf(“%lf%lf%lf”,&a,&b,&c);p=0.5*(a+b+c);s=sqrt(p*(p-a)*(p-b)*(p-c));printf(“s=%.4f\n”,s);}2.23解:第1月开始:a(尚未还清的借款本金)第2月开始:a(1+p)-x第3月开始:(a(1+p)-x)(1+p)-x=a(1+p)2-x(1+p)-x第4月开始:(a(1+p)2-x(1+p)-x)(1+p)-x=a(1+p)3-x(1+p)2-x(1+p)-x…第n+1月开始:a(1+p)n-x((1+p)n-1+(1+p)n-2+…+1)=0于是1)1(1)1(1))1(1(1)1(1)1()1()1(21nnnnnnnpppapppapppax#includestdio.h#includemath.hvoidmain(){doublea,p,x;intn;printf(Inputanp:);scanf(%lf%d%lf,&a,&n,&p);x=a*p*pow(1+p,n)/(pow(1+p,n)-1);printf(x=%.2f\n,x);}输入anp为1000001200.004,输出为1050.91第三章选择结构与循环结构3.3a=2,b=1,c=23.4x=10,y=63.5BCD153.6循环体共执行了3次,输出为:1293.7abcd43.8解:a的值就是循环体被执行的次数先分析内两层循环:最内层循环次数j=11j=22……j=ii总计:1+2+…+i=2)1(ii由于i从1循环到n-1,因此,总的循环次数是4)1(12)12)(1(21212)1(1111112nnnnniiiininini3.9BF3.10#includestdio.hmain(){charc,i,;longs=0;for(i=1;i=10;i++);{c=getchar();if(‘1’=c=’9’c=’1’&&c=’9’)putchar(c);elseif(c==’0’)putchar(‘*’);elses+=(long)c*c;printf(“\ns=%d\n”,s);}printf(“\ns=%ld\n”,s);}3.12解:/*if语句求解*/#include“stdio.h”voidmain(){doubleI,p=0;printf(“InputI=”);scanf(“%lf”,&I);if(I100){p+=(I-100)*0.01;I=100;}if(I60){p+=(I-60)*0.015;I=60;}if(I40){p+=(I-40)*0.03;I=40;}if(I20){p+=(I-20)*0.05;I=20;}if(I10){p+=(I-10)*0.075;I=10;}p+=I*0.1;printf(“p=%f\n”,p);}/*switch语句求解*/#include“stdio.h”voidmain(){doubleI,p=0;printf(“InputI=”);scanf(“%lf”,&I);if(I=100){p+=(I-100)*0.01;I=100;}swith((int)(I/10)){case10:case9:case8:case7:case6:p+=(I-60)*0.015;I=60;case5:case4:p+=(I-40)*0.03;I=40;case3:case2:p+=(I-20)*0.05;I=20;case1:p+=(I-10)*0.075;I=10;default:p+=I*0.1;}printf(“p=%f\n”,p);}3.19解:#include“stdio.h”voidmain(){ints1,s2,s3;floatav;charlevel;while(1){printf(“Inputthreescoresofonestudent:\n”);scanf(“%d%d%d”,&s1,&s2,&s3);if(s10||s20||s30)break;av=(s1+s2+s3)/3.0F;switch((int)(av/10)){case9:case10:level=’A’;break;case8:level=’B’;break;case7:level=’C’;break;case6:level=’D’;break;default:level=’F’;}printf(“level=%c\n”,level);}}第四章基本程序设计技术4.3解:#include“stdio.h”voidmain(){doubles,a;intk,n;printf(“Inputn=”);scanf(“%d”,&n);s=0;a=1;for(k=0;k=n;k++){s+=a;a=(n+1)*a;}printf(“s=%.0f\n”,s);}4.4根据本章打印杨辉三角形例题中组合数的递推计算方法计算组合数:mnC,要求输入n、m值,输出组合数。程序中应对输入的n、m有效性进行检查。解:1m=0mnC=11mnCmmnm0#includestdio.hvoidmain(){intn,m,k,c;printf(Inputnm=);scanf(%d%d,&n,&m);if(n-mm)m=n-m;c=1;for(k=1;k=m;k++)c=c*(n-k+1)/k;printf(c=%d\n,c);}4.5输入x的值,计算要求计算到最后一项绝对值小于10-6时停止计算解:令s=sinx,!)1(21kxakkk,k=1,3,5,…则kkasxk=1ak=22)1(kakkxk=3,5,7,…#includestdio.h#includemath.hvoidmain(){doubles,x,a;intk;printf(Inputx=);scanf(%lf,&x);a=x;s=0;k=1;while(fabs(a)1e-6){s+=a;k+=2;a*=-x*x/k/(k-1);}printf(sin(%.4f)=%.4f\n,x,s);}4.6用公式122122543432122nnnn计算π近似值,计算时取1000个因子解:#includestdio.hvoidmain(){intn;doublepi=1;for(n=2;n=500;n+=2)pi*=(double)n*n/(n-1)/(n+1);pi*=2;printf(pi=%f\n,pi);}4.7解:令221xxa,kkakkb)12(531)2(642,k=1,2,3,4,…设数列和为s,则021kkbxxs,显然,1k=0bk=1122kbkkak0#includestdio.h!7!5!3)sin(753xxxxxsvoidmain(){intn,k;doublex,a,b,s=0;printf(Inputxn=);scanf(%lf%d,&x,&n);a=x*x/(1+x*x);b=1;for(k=1;k=n+1;k++){s+=b;b*=a*(2*k)/(2*k+1);}s*=x/(1+x*x);printf(s=%f\n,s);}程序输入x,n为0.5100,输出为0.4636484.8输入x值(|x|1)432432xxxxs。要求计算到最后一项绝对值小于10-6时停止计算解:#includestdio.h#includemath.hvoidmain(){floatx,s,a;intk;printf(Inputx=);scanf(%f,&x);if(x=-1||x=1){printf(Inputisnotcorrect.\n);return;}a=x;s=0;k=1;while(fabs(a)1e-6){s+=a/k;a*=x;k++;}printf(s=%.4f\n,s);}4.10计算Fibonaui分数序列前n项之和,1321,813,58,35,23,12#includestdio.hmain(){inti,n,t;floata=2,b=1,s=0;scanf(%d,&n);for(i=1;i=n;i++){s=s+a/b;t=a;a=a+b;b=tprintf(s=%10.2\n,s);}4.13韩信点兵:有兵一队,五伍剩一,六伍剩五,七伍剩四,十一伍剩十,问兵几何?解:#includestdio.hvoidmain(){intm;for(m=1;m=10000;m++)if(m%5==1&&m%6==5&&m%7==4&&m%11==10)printf(m=%d\n,m);}1万以内的解有4个:即2111,4421,6721,90414.14解:#includestdio.hvoidmain(){intman,child,women;printf(%8s%8s%8s\n,man,women,child);for(man=1;man17;man++)for(women=1;women25;women++){child=50-3*man-2*women;if(child=1&&man+women+child==30)printf(%8d%8d%8d\n,man,women,child);}}4.15将一张面值100元的人民币兑换为5元,2元,1元和5角的零钞,要求零钞总数不超过40张且每种至少1张,求所有兑换方案。(要求每输出20个解之后暂停输出,按任意键之后,继续输出)解:#includestdio.h#includeconio.hvoidmain(){inti5,i2,i1,i0_5,count=0;for(i5=1;i5=20;i5++)for(i2=1;i237;i2++)for(i1=1;i137;i1++){i0_5=2*(100-5*i5-2*i2-i1);if(i0_5=1&&i5+i2+i1+i0_5=40){printf(%4d%4d%4d%4d\n,i5,i2,i1,i0_5);count++;if(count%20==