资源描述
C语言课后习题答案-第四版-谭浩强(1-7)第一早#include int main () printf (*nn);printf( Very Good!nn);printf (*n,return 0;#include int main()int a,b,c,max;printf(please input a,b,c:n);scanf(%d,%d,%d”,&a,&b,&c);max=a;if (maxb)max=b;if (maxc)max=c;printf(The largest number is %dn,max);return 0;第3章#include #include int main()(float p,r,n;r=0.1;n=10;p=pow(1+r,n);printf(p=%fn,p);return 0;#include #include int main() (float r5,r3,r2,r1,r0,p,p1,p2,p3,p4,p5;p=1000;r5=0.0585;r3=0.054;r2=0.0468;r1=0.0414;r0=0.0072;p1=p*(1+r5)*5);/ 一次存 5 年期p2=p*(1+2*r2)*(1+3*r3);/先存2年期,到期后将本息再存3年期p3=p*(1+3*r3)*(1+2*r2);/先存3年期,到期后将本息再存2年期p4=p*pow(1+r1,5);/存1年期,到期后将本息存再存1年期,连续存5次p5=p*pow(1+r0/4,4*5);/存活期存款。活期利息每一季度结算一次printf(p1=%fn”,p1);/输出按第1方案得到的本息和printf(p2=%fn”,p2);/输出按第2方案得到的本息和printf(p3=%fn,p3);/输出按第3方案得到的本息和printf(p4=%fn,p4);/输出按第4方案得到的本息和printf(p5=%fn,p5);/输出按第5方案得到的本息和return 0;#include #include int main()(double r5,r3,r2,r1,r0,p,p1,p2,p3,p4,p5;p=1000;r5=0.0585;r3=0.054;r2=0.0468;r1=0.0414;r0=0.0072;p1=p*(1+r5)*5);/ 一次存 5 年期p2=p*(1+2*r2)*(1+3*r3);/先存2年期,到期后将本息再存3年期p3=p*(1+3*r3)*(1+2*r2);/先存3年期,到期后将本息再存2年期p4=p*pow(1+r1,5);/存1年期,到期后将本息存再存1年期,连续存5次p5=p*pow(1+r0/4,4*5);/存活期存款。活期利息每一季度结算一次printf(p1=%fn,p1);/输出按第1方案得到的本息和printf(p2=%fn,p2);/输出按第2方案得到的本息和printf(p3=%fn”,p3);/输出按第3方案得到的本息和printf(p4=%fn”,p4);/输出按第4方案得到的本息和printf(p5=%fn,p5);/输出按第5方案得到的本息和return 0;#include int main()(float r5,r3,r2,r1,r0,p,p1,p2,p3,p4,p5;p=1000;r5=0.0585;r3=0.054;r2=0.0468;r1=0.0414;r0=0.0072;p1=p*(1+r5)*5);/一次存5年期p2=p*(1+2*r2)*(1+3*r3);/先存2年期,到期后将本息再存3年期p3=p*(1+3*r3)*(1+2*r2);/先存3年期,到期后将本息再存2年期p4=p*pow(1+r1,5);/存1年期,到期后将本息存再存1年期,连续存5次p5=p*pow(1+r0/4,4*5);/存活期存款。活期利息每一季度结算一次printf(p1=%10.2fn”,p1);/输出按第1方案得到的本息和printf(p2=%10.2fn”,p2);/输出按第2方案得到的本息和printf(p3=%10.2fn,p3);/输出按第3方案得到的本息和printf(p4=%10.2fn”,p4);/输出按第4方案得到的本息和printf(p5=%10.2fn”,p5);/输出按第5方案得到的本息和return 0;#include #include int main()(float d=300000,p=6000,r=0.01,m;m=log10(p/(p-d*r)/log10(1+r);printf(m=%6.2fn”,m);return 0;#include 一int main()(int c1,c2;c1=197;c2=198;printf(c1=%c,c2=%cn”,c1,c2);printf(c1=%d, c2=%dn”,c1,c2);return 0;int main()(int a,b;float x,y;char c1,c2;scanf(a=%d b=%d”,&a,&b);scanf(%f %e”,&x,&y);scanf(%c%c”,&c1,&c2);printf(a=%d,b=%d,x=%f,y=%f,c1=%c,c2=%cn,a,b,x,y,c1,c2);return 0;#include int main()(char c1=C,c2=h,c3=i,c4=n,c5=a;c1=c1+4;c2=c2+4;c3=c3+4;c4=c4+4;c5=c5+4;printf(passwor is %c%c%c%c%cn”,c1,c2,c3,c4,c5);return 0;#include int main ()(float h,r,l,s,sq,vq,vz;float pi=3.141526; printf(请输入圆半 径r,圆柱高h:);scanf(%f,%f”,&r,&h);要求输入圆半径r和圆柱高hl=2*pi*r;计算圆周长ls=r*r*pi;计算圆面积ssq=4*pi*r*r;/计算圆球表面积 sqvq=3.0/4.0*pi*r*r*r;/计算圆球体积 vqvz=pi*r*r*h;/计算圆柱体积 vzprintf(圆周长为:l=%6.2fn”,l);printf(圆面积为:s=%6.2fn”,s);printf(圆球表面积为:sq=%6.2fn”,sq);printf(圆球体积为:v=%6.2fn”,vq);printf(圆柱体积为:vz=%6.2fn”,vz);return 0;int main()char c1,c2;printf(-请输入两个字符c1,c2:);c1=getchar();c2=getchar();printf(用 putchar语句输出结果为:,putchar(c1);putchar(c2);printf(n);printf(用 printf语句输出结果为:,printf(%c %cn”,c1,c2);return 0;#include int main()int c1,c2;/整型定义printf(请输入两个整数c1,c2:);scanf(%d,%d”,&c1,&c2);printf(-按字符输出结果:n);/定义字符型变量定义整型变量赋值给整型变量/定义为字符型printf(%c,%cn”,c1,c2);printf(按 ASCII 码输出结果为:n);printf(%d,%dn”,c1,c2);return 0;#include int main()char c1,c2;int i1,i2;printf(-请输入两个字符c1,c2:);scanf(%c,%c”,&c1,&c2);i1=c1;i2=c2;printf(-按字符输出结果:n);printf(%c,%cn”,i1,i2);printf(按整数输出结果:n);printf(%d,%dn”,c1,c2);return 0;#include int main()char c1,c2;定义为整型printf(请输入两个整数i1,i2:);scanf(%d,%d”,&i1,&i2);c1=i1;/将整数赋值给字符变量c2=i2;printf(-按字符输出结果:n);printf(%c,%cn”,c1,c2);printf(按整数输出结果:n);printf(%d,%dn”,c1,c2);return 0;第四章#include int main()int a,b,c;printf( ”请输入三个整数:);scanf(d,%d,%d”,&a,&b,&c);if (ab)if (bc)printf(max=%dn,c);elseprintf(max=%dn”,b);else if (ac)printf(max=%dn,c);elseprintf(max=%dn,a);return 0;#include int main()( int a,b,c,temp,max;printf(请输入三个整数:”);scanf(%d,%d,%d”,&a,&b,&c);temp=(ab)?a:b;/*将a和b中的大者存入temp中*/max=(tempc)?temp:c;/*将a和b中的大者与c比较,取最大者*/printf(三个整数的最大数是dn”,max);return 0; #include #include #define M 1000int main()int i,k;printf(请输入一个小于d的整数i:,M);scanf(%d”,&i);while (iM)printf(-输入的数不符合要求,请重新输入一个小于d的整数i:,M); scanf(%d”,&i);k=sqrt(i);printf(%d的平方根的整数部分是:dn”,i,k);return 0;#include #include #defineM 1000 int main()int i,k;printf(请输入一个小于d的整数i:,M);scanf(%d”,&i);if (iM)printf(-输入的数不符合要求,请重新输入一个小于d的整数i:,M);scanf(%d”,&i);k=sqrt(i);printf(%d的平方根的整数部分是:dn”,i,k);return 0;#include int main() int x,y;printf(输入 x:);scanf(%d”,&x);if(x1)/* x1 */ y=x;printf(x=%3d, y=x=%dn” ,x,y);else if(x10)/* 1=x=10 */ y=3*x-11;printf(x=%d, y=3*x-11=%dn”,x,y);return0; #include int main()int x,y;printf(enter x:);scanf(%d”,&x);y=-1;if(x!=0)if(x0)y=1;elsey=0;printf(x=%d,y=%dn”,x,y);return 0;#include int main()int x,y;printf(please enter x:);scanf(%d”,&x);y=0;if(x=0)if(x0) y=1;else y=-1;printf(x=%d,y=%dn”,x,y);return 0;#include int main() float score;char grade;printf(-请输入学生成绩:);scanf(%f”,&score);while (score100|score0)scanf(%f”,&score);switch(int)(score/10)(case 10:case 9: grade=A;break;case 8: grade=B;break;case 7: grade=C;break;case 6: grade=D;break;case 5:case 4:case 3:case 2:case 1:case 0: grade=E;printf(成绩是 %5.1f,相应的等级是 cn ”,score,grade);return 0;#include #include int main()/分别代表个位,十位,百位,千int num,indiv,ten,hundred,thousand,ten_thousand,place;位,万位和位数printf(请输入一个整数(0-99999):);scanf(%d”,&num);if (num9999)place=5;else if (num999)place=4;else if (num99)place=3;else if (num9)place=2;else place=1;printf(位数:dn”,place); printf(每位数字为:,ten_thousand=num/10000;thousand=(int)(num-ten_thousand*10000)/1000;hundred=(int)(num-ten_thousand*10000-thousand*1000)/100; ten=(int)(num-ten_thousand*10000-thousand*1000-hundred*100)/10; indiv=(int)(num-ten_thousand*10000-thousand*1000-hundred*100-ten*10);switch(place)(case 5:printf(%d,%d,%d,%d,%d”,ten_thousand,thousand,hundred,ten,indiv);printf(n反序数字为:);printf(%d%d%d%d%dn”,indiv,ten,hundred,thousand,ten_thousand);break;case 4:printf(%d,%d,%d,%d”,thousand,hundred,ten,indiv);printf(n反序数字为:);printf(%d%d%d%dn,indiv,ten,hundred,thousand);break;case 3:printf(%d,%d,%d”,hundred,ten,indiv);printf(n反序数字为:);printf(%d%d%dn,indiv,ten,hundred);break;case 2:printf(%d,%d”,ten,indiv);printf(n反序数字为:);printf(%d%dn”,indiv,ten);break;case 1:printf(%d”,indiv);printf(n反序数字为:);printf(%dn,indiv);break;return0; #includeint main()int i;double bonus,bon1,bon2,bon4,bon6,bon10;bon1=100000*0.1;bon2=bon1+100000*0.075;bon4=bon2+100000*0.05;bon6=bon4+100000*0.03;bon10=bon6+400000*0.015; printf(请输入利润i:);scanf(%d”,&i);if (i=100000)bonus=i*0.1;else if (i=200000)bonus=bon1+(i-100000)*0.075;else if (i=400000)bonus=bon2+(i-200000)*0.05;else if (i=600000)bonus=bon4+(i-400000)*0.03;else if (i=1000000)bonus=bon6+(i-600000)*0.015;elsebonus=bon10+(i-1000000)*0.01;printf(奖金是:%10.2fn,bonus);return 0;#include int main()int i;double bonus,bon1,bon2,bon4,bon6,bon10;int branch;bon1=100000*0.1;bon2=bon1+100000*0.075;bon4=bon2+200000*0.05;bon6=bon4+200000*0.03;bon10=bon6+400000*0.015;printf(请输入利润i:);scanf(d”,&i);branch=i/100000;if (branch10) branch=10;switch(branch) case 0:bonus=i*0.1;break;case 1:bonus=bon1+(i-100000)*0.075;break;case 2:case 3: bonus=bon2+(i-200000)*0.05;break;case 4:case 5: bonus=bon4+(i-400000)*0.03;break;case 6:case 7:case 8:case 9: bonus=bon6+(i-600000)*0.015;break;case 10: bonus=bon10+(i-1000000)*0.01;printf(奖金是 10.2fn”,bonus);return 0;#include int main()int t,a,b,c,d; printf(-请输入四个数:);scanf(d,%d,%d,%d”,&a,&b,&c,&d);printf(a=%d,b=%d,c=%d,d=%dn”,a,b,c,d);if (ab) t=a;a=b;b=t;if (ac)( t=a;a=c;c=t;if (ad)( t=a;a=d;d=t;if (bc)( t=b;b=c;c=t;if (bd)( t=b;b=d;d=t;if (cd)( t=c;c=d;d=t;printf(排序结果如下:n);printf(%d %d %d %d n,a,b,c,d);return 0;#include int main()int h=10;float x1=2,y1=2,x2=-2,y2=2,x3=-2,y3=-2,x4=2,y4=-2,x,y,d1,d2,d3,d4;printf(-请输入一个点(x,y):);scanf(%f,%f”,&x,&y);d1=(x-x4)*(x-x4)+(y-y4)*(y-y4); /*求该点到各中心点距离 */ d2=(x-x1)*(x-x1)+(y-y1)*(y-y1);d3=(x-x2)*(x-x2)+(y-y2)*(y-y2);d4=(x-x3)*(x-x3)+(y-y3)*(y-y3);if (d11 & d21 & d31 & d41) h=0; /*判断该点是否在塔外 */printf(该点高度为 %dn,h);return 0;-j-* Vr.弟五早#include #include /程序中用到数学函数fabs,应包含头文件math.nint main()int sign=1,count=0;/ sign用来表示数值的符号,count用来统计循环次数double pi=0.0,n=1.0,term=1.0;/ pi开始代表多项式的值,最后代表兀的值,n代表分母,term代表当前项的值while(fabs(term)=1e-8)/检查当前项term的绝对值是否大于或等于10的(-6)次方pi=pi+term;/把当前项term累加到pi中n=n+2;/ n+2是下一项的分母sign=-sign;/ sign代表符号,下一项的符号与上一项符号相反term=sign/n;/ 求出下一项的值 termcount+;/ count 累加 1pi=pi*4;/多项式的和pi乘以4,才是兀的近似值printf(pi=%10.8fn”,pi);/ 输出 n 的近似值printf(count=%dn,count); / 输出循环次数return 0;#include int main()int p,r,n,m,temp;printf(请输入两个正整数n,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(它们的最大公约数为:dn”,n);printf(它们的最小公约数为:dn”,p/n);return 0;#include int main()char c;int letters=0,space=0,digit=0,other=0;printf( ”请输入一行字符:n);while(c=getchar()!=n)if (c=a & c=A & c=0 & c=9)digit+;elseother+;printf(字母数:dn 空格数:dn 数字数:dn 其它字符数:dn”,letters,space,digit,other);return 0; #include int main()int a,n,i=1,sn=0,tn=0;printf(a,n=:);scanf(d,%d”,&a,&n);while (i=n)tn=tn+a; /*赋值后的tn为i个a组成数的值*/sn=sn+tn; /*赋值后的sn为多项式前i项之和*/a=a*10;+i;printf(a+aa+aaa+.=%dn,sn);return 0;#include int main()double s=O,t=l;int n;for (n=l;nv=20;n+)(t=t*n;s=s+t;printf(l!+2!+.+20!=%22.15en,s);return 0; #include int main()(int nl=100,n2=50,n3=10;double k,sl=0,s2=0,s3=0;for (k= 1 ;k=nl ;k+) /*计算 1 至0 100 的和*/sl=sl+k;for (k=l;kv=n2;k+) /*计算1到50各数的平方和*/s2=s2+k*k;for (k=1;k=n3;k+) /*计算 1 到 10 的各倒数和*/s3=s3+1/k;printf(sum=%15.6fn”,s1+s2+s3);return 0;#include int main()int i,j,k,n;printf(parcissus numbers are );for (n=100;n1000;n+)i=n/100;j=n/10-i*10;k=n%10;if (n=i*i*i + j*j*j + k*k*k)printf(%d ,n);printf(n);return 0;#define M 1000 /* 定义寻找范围 */ #include int main()int k1,k2,k3,k4,k5,k6,k7,k8,k9,k10;int i,a,n,s;for (a=2;a=M;a+)/* a是2-1000之间的整数,检查它是否完数*/n=0;/* n用来累计a的因子的个数*/s=a;/* s用来存放尚未求出的因子之和,开始时等于a */for (i=1;i1)printf(%d,%d”,k1,k2);/* n1表示a至少有2个因子*/if (n2)因子 */printf(,%d”,k3);/* n2表示至少有3个因子,故应再输出一个if (n3)因子 */printf(,%d”,k4);/* n3表示至少有4个因子,故应再输出一个if (n4)printf(,%d”,k5);/*以下类似*/if (n5)printf(,%d,k6);if (n6)printf(,%d,k7);if (n7) printf(,%d”,k8);if (n8) printf(,%d”,k9);if (n9) printf(,%d”,k10);printf(n);return0; #includeint main()(int m,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(%d,its factors are ,m);for (i=1;im;i+)if (m%i=0) printf(%d ,i);printf(n);return 0;#includeint i,n=20;double a=2,b=1,s=0,t;for (i=1;i=n;i+)s=s+a/b;t=a,a=a+b,b=t;printf(sum=%16.10fn,s);return 0;#include int main()double sn=100,hn=sn/2;int n;for (n=2;n=10;n+)sn=sn+2*hn;/*第n次落地时共经过的米数*/hn=hn/2;/*第n次反跳高度*/printf(”第10次落地时共经过f米n,sn);printf(第 10 次反弹f 米n,hn);return 0;#include int main()int day,x1,x2;day=9; x2=1;while(day0)x1=(x2+1)*2; /*第1天的桃子数是第2天桃子数加1后的2倍.*/x2=x1;day-;printf(total=%dn,x1);return 0; #include #include int main()float a,x0,x1;printf(enter a positive number:);scanf(%f”,&a);x0=a/2;x1=(x0+a/x0)/2;dox0=x1;x1=(x0+a/x0)/2; while(fabs(x0-x1)=1e-5);printf(The square root of %5.2f is %8.5fn”,a,x1);return 0; #include #include int main() doublex1,x0,f,f1;x1=1.5;dox0=x1; f=(2*x0-4)*x0+3)*x0-6;f1=(6*x0-8)*x0+3;x1=x0-f/f1; while(fabs(x1-x0)=1e-5);printf(The root of equation is %5.2fn”,x1);return 0; #include #include int main()(float x0,x1,x2,fx0,fx1,fx2;do(printf(enter x1 & 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)=1e-5);printf(x=%6.2fn,x0);return 0;#include int main()int i,j,k;for (i=0;i=3;i+) for(j=0;j=2-i;j+)printf();for (k=0;k=2*i;k+)printf(*”);printf(n);for (i=0;i=2;i+)for (j=0;j=i;j+)printf();for (k=0;k=4-2*i;k+)printf(*);printf(n);#include int main()char i,j,k;/*是a的对手;j是b的对手;k是c的对手*/for (i=x;i=z;i+)for (j=x;j=z;j+)if (i!=j)for (k=x;k=z;k+)if (i!=k & j!=k)if (i!=x & k!=x & k!=z) printf(A-%cnB-%cnC-%cn”,i,j,k);return 0;弟六早#include #include int main()int i,j,n,a101;for (i=1;i=100;i+)ai=i;a1=0;for (i=2;isqrt(100);i+)for (j=i+1;j=100;j+)if(ai!=0 & aj!=0)if (aj%ai=0)aj=0;printf(n);for (i=2,n=0;i=100;i+) if(ai!=0)printf(%5d”,ai);n+;if(n=10)printf(n);n=0;printf(n);return 0;#include int main()int i,j,min,temp,a11;printf(enter data:n);for (i=1;i=10;i+)printf(a%d=,i);scanf(%d”,&ai);printf(n);printf(The orginal numbers:n);for (i=1;i=10;i+)printf(%5d”,ai);printf(n);for (i=1;i=9;i+)for (j=i+1;jaj) min=j;temp=ai;ai=amin;amin=temp;printf(nThe sorted numbers:n);for (i=1;i=10;i+)printf(%5d”,ai);printf(n);return 0;#include int main()int a33,sum=0;int i,j;printf(enter data:n);for (i=0;i3;i+)for (j=0;j3;j+)for (i=0;i3;i+)sum=sum+aii;printf(sum=%6dn,sum);return 0;#include int main()( int a11=1,4,6,9,13,16,19,28,40,100;int temp1,temp2,number,end,i,j;printf(array a:n);for (i=0;iend)a10=number;else(if (ainumber)temp1=ai;ai=number;for (j=i+1;j11;j+)(temp2=aj;aj=temp1;temp1=temp2;break;printf(Now array a:n);for (i=0;i11;i+)printf(%5d”,ai);printf(n);return 0;#include #define N 5int main() ( int aN,i,temp;printf(enter array a:n);for (i=0;iN;i+)scanf(%d”,&ai);printf(array a:n);for (i=0;iN;i+)printf(%4d”,ai);for (i=0;iN/2;i+)循环的作用是将对称的元素的值互换( temp=ai;ai=aN-i-1;aN-i-1=temp;printf(nNow,array a:n);for (i=0;iN;i+)printf(%4d”,ai);printf(n);return 0;#include #define N 10int main() int i,j,aNN;for (i=0;iN;i+)aii=1;ai0=1;for (i=2;iN;i+)for (j=1;j=i-1;j+) aij=ai-1j-1+ai-1j;for (i=0;iN;i+) for(j=0;j=i;j+)printf(%6d,aij);printf(n);printf(n);return 0;#include int main() int a1515,i,j,k,p,n;p=1;while(p=1)printf(enter n(n=1-15):);scanf(%d”,&n);if (n!=0) & (n=15) & (n%2!=0) p=0;for (i=1;i=n;i+) for(j=1;j=n;j+)aij=0;j=n/2+1;a1j=1;for (k=2;k=n*n;k+)i=i-1;j=j+1;if (in)i=i+2;j=j-1;elseif (in) j=1;if (aij=0)aij=k;elsei=i+2;j=j-l;aij=k;for (i=1;i=n;i+)for (j=1;j=n;j+)printf(%5d”,aij);printf(n);return 0;#include #define N 4#define M 5 /* 数组为 4 行 5 列 */ int main()int i,j,k,aNM,max,maxj,flag;printf(please input matrix:n);for (i=0;iN;i+)/*输入数组*/scanf(%d,&aij);for (i=0;iN;i+)max=ai0;/*开始时假设ai0最大*/maxj=0;/*将列号0赋给maxj保存*/for (j=0;jmax)max=aij;/*将本行的最大数存放在max中*/maxj=j;/*将最大数所在的列号存放在maxj中*/flag=1;/*先假设是鞍点,以flag为1代表*/for (k=0;kakmaxj)/*将最大数和其同列兀素相比*/flag=0;*/*如果max不是同列最小,表示不是鞍点令flagl为0continue;if(flag)/*如果flagl为1表示是鞍点*/printf(a%d%d=%dn”,i,maxj,max); /* 输出鞍点的值和所在行列号 */ break;if(!flag)/*如果flag为0表示鞍点不存在*/printf(It is not exist!n);return 0;#include #define N 15int main()( int i,number,top,bott,mid,loca,aN,flag=1,sign;char c;printf(enter data:n);scanf(%d”,&a0);i=1;while(i=ai-1)i+;elseprintf(enter this data again:n);printf(n);for (i=0;iN;i+)printf(%5d,ai);while(flag)(printf(input number to look for:);scanf(%d”,&number);sign=0;top=0;/top是查找区间的起始位置bott=N-1;/bott是查找区间的最末位置if (numberaN-1) /要查的数不在查找区间内loca=-1;/表示找不到while (!sign) & (top=bott)(mid=(bott+top)/2;if (number=amid)loca=mid;printf(Has found %d, its position is %dn”,number,loca+1);sign=1;else if (numberamid)bott=mid-1;elsetop=mid+1;if(!sign|loca=-1)printf(cannot find %d.n,number);printf(continu or not(Y/N)?);scanf( %c”,&c);if (c=N|c=n)flag=0;return 0;#include int main()(int i,j,upp,low,dig,spa,oth;char text380;upp=low=dig=spa=oth=0;for (i=0;i3;i+)(printf(please input li
展开阅读全文