《谭浩强C语言程序设计习题答案》由会员分享,可在线阅读,更多相关《谭浩强C语言程序设计习题答案(103页珍藏版)》请在金锄头文库上搜索。
1、1谭浩强谭浩强 C C 语言程序设计习题参考答案语言程序设计习题参考答案第一章1.6main()int a,b,c,max;printf(“input three numbers:n“);scanf(“%d,%d,%d“,max=a;if(maxb)?a:b;max=(ctemp)?c:temp;printf(“max=%d“,max);4.5main()int x,y;scanf(“%d“,if(x=0else if(num999) place=4;else if(num99) place=3;else if(num9) place=2;else place=1;printf(“place=
2、%dn“,place);ten_thousand=num/10000;thousand=(num-ten_thousand*10000)/1000;hundred=(num-ten_thousand*10000-thousand*1000)/100;ten=(num-ten_thousand*10000-thousand*1000-hundred*100)/10;indiv=num-ten_thousand*10000-thousand*1000-hundred*100-ten*10;10switch(place)case 5:printf(“%d,%d,%d,%d,%dn“,ten_thou
3、sand,thousand,hundred,ten,indiv);printf(“%d,%d,%d,%d,%dn“,indiv,ten,hundred,thousand,ten_thousand);break;case 4:printf(“%d,%d,%d,%dn“,thousand,hundred,ten,indiv);printf(“%d,%d,%d,%dn“,indiv,ten,hundred,thousand);break;case 3:printf(“%d,%d,%dn“,hundred,ten,indiv);printf(“%d,%d,%dn“,indiv,ten,hundred)
4、;break;case 2:printf(“%d,%dn“,ten,indiv);printf(“%d,%dn“,indiv,ten);break; case 1:printf(“%dn“,indiv);printf(“%dn“,indiv);114.8main()long i;float bonus,bon1,bon2,bon4,bon6,bon10;bon1=100000*0.1;bon2=bon1+100000*0.075;bon4=bon2+200000*0.05;bon6=bon4+200000*0.03;bon10=bon6+400000*0.015;scanf(“%ld“,if(
5、i10)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 7case 8:case 9:bonus=bon6+(i-600000)*0.015;break;case 10:bonus=bon10+(i-1000000)*0.01;13printf(“bonu
6、s=%10.2f“,bonus);4.9main()int t,a,b,c,d;scanf(“%d,%d,%d,%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(“%d %d %d %dn“,a,b,c,d);4.10main()int h=10;float x,y,x0=2,y0=2,d1,d2,d3,d4;scanf(“%f,%f“,d1=(x-x0)*(x-x0)+(y-y0)*(y-y0);d2=(x
7、-x0)*(x-x0)+(y+y0)*(y+y0);14d3=(x+x0)*(x+x0)+(y-y0)*(y-y0);d4=(x+x0)*(x+x0)+(y+y0)*(y+y0);if(d11printf(“h=%d“,h);第五章 循环控制5.1main()int a,b,num1,num2,temp;scanf(“%d,%d“,if(num1=aif(n2)printf(“,%d“,k2);if(n3)printf(“,%d“,k3);if(n4)printf(“,%d“,k4);if(n5)printf(“,%d“,k5);if(n6)printf(“,%d“,k6);if(n7)pri
8、ntf(“,%d“,k7);19if(n8)printf(“,%d“,k8);if(n9)printf(“,%dn“,k9);main()static int k10;int i,j,n,s;for(j=2;j0)x1=(x2+1)*2;x2=x1;day-;printf(“x1=%dn“,x1);5.11#include“math.h“main()float a,xn0,xn1;scanf(“%f“,xn0=a/2;xn1=(xn0+a/xn0)/2;22do xn0=xn1;xn1=(xn0+a/xn0)/2;while(fabs(xn0-xn1)=1e-5);printf(“a=%5.2
9、fn,xn1=%8.2fn“,a,xn1);5.12#include“math.h“main()float x,x0,f,f1;x=1.5;dox0=x;f=(2*x0-4)*x0+3)*x0-6;f1=(6*x0-8)*x0+3;x=x0-f/f1;while(fabs(x-x0)=1e-5);printf(“x=%6.2fn“,x);5.1323#include“math.h“main()float x0,x1,x2,fx0,fx1,fx2;doscanf(“%f,%f“,fx1=x1*(2*x1-4)*x1+3)-6;fx2=x2*(2*x2-4)*x2+3)-6;while(fx1*f
10、x20);dox0=(x1+x2)/2;fx0=x0*(2*x0-4)*x0+3)-6;if(fx0*fx1)=1e-5);24printf(“x0=%6.2fn“,x0);5.14main()int i,j,k;for(i=0;i#define N 101main() int i,j,line,aN;for (i=2;iaj)min=j;27temp=ai;ai=amin;amin=temp;for(i=0;iend) a10=number;else for(i=0;inumber)temp1=ai;ai=number;for(j=i+1;jn)i=i+2;j=j-1;elseif(in)j
11、=1;if(aij=0)aij=k;elsei=i+2;j=j-1;aij=k;for(i=1;iakmaxj)flag1=0;if(flag1) printf(“na%d%d=%dn“,i,maxj,max);33flag2=1;if(!flag2) printf(“NOT“);6.9#include#define N 15main()int i,j,number,top,bott,min,loca,aN,flag;char c;for(i=0;iaN-1)loca=-1;while(loca=0)char ch80,tran80;gets(ch);i=0;while(chi!=0)37if
12、(chi=A)char s1100,s2100;gets(s1);gets(s2);i=0;while(s1i=s2i)if(s1i=0else resu=s1i-s2i;printf(“s1=%s,s2=%s,resu=%dn“,s1,s2,resu);6.15#include“stdio.h“main()char from80,to80;int i;scanf(“%s“,from);for(i=0;iv)t=u;u=v;v=t;a=u;b=v;while(r=b%a)!=0)b=a;a=r;return(a);lcd(u,v,h)int u,v,h;return(u*v/h);main()
13、int u,v,h,l;scanf(“%d,%d“,h=hcf(u,v);printf(“H.C.F=%dn“,h);40l=lcd(u,v,h);printf(“L.C.D=%dn“,l);7.2#include“math.h“float x1,x2,disc,p,q;greater_than_zero(a,b)float a,b;x1=(-b+sqrt(disc)/(2*a);x2=(-b-sqrt(disc)/(2*a);equal_to_zero(a,b)flaot a,b;x1=x2=-b/(2*a);smaller_than_zero(a,b)float a,b;p=-b/(2*a
14、);q=sqrt(-disc)/(2*a);main()float a,b,c;scanf(“%f,%f,%f“,41disc=b*b-4*a*c;if(fabs(disc)0)greater_than_zero(a,b);printf(“x1=%5.2ftx2=%5.2fn“,x1,x2);elsesmaller_than_zero(a,b);printf(“x1=%5.2f+%5.2fitx2=%5.2f-%5.2fin“,p,q,p,q);7.3main()int number;scanf(“%d“,if(prime(number)printf(“yes“);else42printf(“
15、no“);int prime(number)int number;int flag=1,n;for(n=2;n0;i-)stri*2=stri;stri*2-1= ;printf(“%sn“,str);7.9int alph,digit,space,others;main()char text80;gets(text);alph=0,digit=0,space=0,others=0;47count(text);printf(“nalph=%d,digit=%d,space=%d,others=%dn“,alph,digit,space,others);count(str)char str;int i;for(i=0;stri!=0;i+)if(stri=aplace=point;len=0;49return(place);main()int i;char line100;gets(line);for(i=longest(line);alph(linei);i+)printf(“%c“,linei);printf(“n“);7.11#define N 10char strN;main()int