• 439.07 KB
  • 2022-04-22 11:32:19 发布

《C语言程序设计》谭浩强第三版课后答案

  • 25页
  • 当前文档由用户上传发布,收益归属用户
  1. 1、本文档共5页,可阅读全部内容。
  2. 2、本文档内容版权归属内容提供方,所产生的收益全部归内容提供方所有。如果您对本文有版权争议,可选择认领,认领后既往收益都归您。
  3. 3、本文档由用户上传,本站不保证质量和数量令人满意,可能有诸多瑕疵,付费之前,请仔细先通过免费阅读内容等途径辨别内容交易风险。如存在严重挂羊头卖狗肉之情形,可联系本站下载客服投诉处理。
  4. 文档侵权举报电话:19940600175。
'课后答案网www.khdaw.comC语言设计谭浩强第三版的课后习题答案(一)1.5请参照本章例题,编写一个C程序,输出以下信息:************VeryGoodj!************解:main(){printf("************n");printf("n");printf("VeryGood!n");printf("n");printf("************n");}1.6编写一个程序,输入abc三个值,输出其中最大者。解:main(){inta,b,c,max;printf("请输入三个数a,b,c:n");scanf("%d,%d,%d",&a,&b,&c);max=a;if(maxvoidmain(){charc1="a",c2="b",c3="c",c4="101",c5="116";printf("a%cb%ctc%ctabcn",c1,c2,c3);printf("tb%c%cn",c4,c5);解:程序的运行结果为:aabbccabcAN3.7将"China"译成密码.密码规律:用原来的字母后面第4个字母代替原来的字母,例如,字母"A"后面第4个字母是"E",用"E"代替"A".因此,"China"应译为"Glmre".请编一程序,用赋初值的议程使c1,c2,c3,c4,c5分别变成"G","1","m","r","e",并输出.main(){charc1="C",c2="h",c3="i",c4="n",c5="a";c1+=4;c2+=4;c3+=4;c4+=4;c5+=4;printf("密码是%c%c%c%c%cn",c1,c2,c3,c4,c5);}3.8例3.6能否改成如下:#includevoidmain(){intc1,c2;(原为charc1,c2)c1=97;c2=98;printf("%c%cn",c1,c2);printf("%d%dn",c1,c2);}解:可以.因为在可输出的字符范围内,用整型和字符型作用相同.3.9求下面算术表达式的值.(1)x+a%3*(int)(x+y)%2/4=2.5(x=2.5,a=7,y=4.7)(2)(float)(a+b)/2+(int)x%(int)y=3.5(设a=2,b=3,x=3.5,y=2.5)3.10写出下面程序的运行结果:#includevoidmain(){inti,j,m,n;i=8;j=10;m=++i; 课后答案网www.khdaw.comn=j++;printf("%d,%d,%d,%dn",i,j,m,n);}解:结果:9,11,9,10第4章4.4.a=3,b=4,c=5,x=1.2,y=2.4,z=-3.6,u=51274,n=128765,c1="a",c2="b".想得到以下的输出格式和结果,请写出程序要求输出的结果如下:a=3b=4c=5x=1.200000,y=2.400000,z=-3.600000x+y=3.60y+z=-1.20z+x=-2.40u=51274n=128765c1="a"or97(ASCII)c2="B"or98(ASCII)解:main(){inta,b,c;longintu,n;floatx,y,z;charc1,c2;a=3;b=4;c=5;x=1.2;y=2.4;z=-3.6;u=51274;n=128765;c1="a";c2="b";printf("n");printf("a=%2db=%2dc=%2dn",a,b,c);printf("x=%8.6f,y=%8.6f,z=%9.6fn",x,y,z);printf("x+y=%5.2fy=z=%5.2fz+x=%5.2fn",x+y,y+z,z+x);printf("u=%6ldn=%9ldn",u,n);printf("c1="%c"or%d(ASCII)n",c1,c2);printf("c2="%c"or%d(ASCII)n",c2,c2);}4.5请写出下面程序的输出结果.结果:575767.856400,-789.12396267.856400,-789.12396267.86,-789.12,67.856400,-789.123962,67.856400,-789.1239626.785640e+001,-7.89e+002A,65,101,411234567,4553207,d68765535,17777,ffff,-1COMPUTER,COM 课后答案网www.khdaw.com4.6用下面的scanf函数输入数据,使a=3,b=7,x=8.5,y=71.82,c1="A",c2="a",问在键盘上如何输入?main(){inta,b;floatx,y;charc1,c2;scanf("a=%db=%d,&a,&b);scanf("x=%fy=%e",&x,&y);scanf("c1=%cc2=%c",&c1,&c2);}解:可按如下方式在键盘上输入:a=3b=7x=8.5y=71.82c1=Ac2=a说明:在边疆使用一个或多个scnaf函数时,第一个输入行末尾输入的"回车"被第二个scanf函数吸收,因此在第二三个scanf函数的双引号后设一个空格以抵消上行入的"回车".如果没有这个空格,按上面输入数据会出错,读者目前对此只留有一初步概念即可,以后再进一步深入理解.4.7用下面的scanf函数输入数据使a=10,b=20,c1="A",c2="a",x=1.5,y=-3.75,z=57.8,请问在键盘上如何输入数据?scanf("%5d%5d%c%c%f%f%*f%f",&a,&b,&c1,&c2,&y,&z);解:main(){inta,b;floatx,y,z;charc1,c2;scanf("%5d%5d%c%c%f%f",&a,&b,&c1,&c2,&x,&y,&z);}运行时输入:1020Aa1.5-3.75+1.5,67.8注解:按%5d格式的要求输入a与b时,要先键入三个空格,而后再打入10与20。%*f是用来禁止赋值的。在输入时,对应于%*f的地方,随意打入了一个数1.5,该值不会赋给任何变量。3.8设圆半径r=1.5,圆柱高h=3,求圆周长,圆面积,圆球表面积,圆球体积,圆柱体积,用scanf输入数据,输出计算结果,输出时要求有文字说明,取小数点后两位数字.请编程.解:main(){floatpi,h,r,l,s,sq,vq,vz;pi=3.1415926;printf("请输入圆半径r圆柱高h:n");scanf("%f,%f",&r,&h);l=2*pi*r; 课后答案网www.khdaw.coms=r*r*pi;sq=4*pi*r*r;vq=4.0/3.0*pi*r*r*r;vz=pi*r*r*h;printf("圆周长为:=%6.2fn",l);printf("圆面积为:=%6.2fn",s);printf("圆球表面积为:=%6.2fn",sq);printf("圆球体积为:=%6.2fn",vz);}4.9输入一个华氏温度,要求输出摄氏温度,公式为C=5/9(F-32),输出要有文字说明,取两位小数.解:main(){floatc,f;printf("请输入一个华氏温度:n");scanf("%f",&f);c=(5.0/9.0)*(f-32);printf("摄氏温度为:%5.2fn",c);}第五章逻辑运算和判断选取结构5.4有三个整数a,b,c,由键盘输入,输出其中最大的数.main(){inta,b,c;printf("请输入三个数:");scanf("%d,%d,%d",&a,&b,&c);if(ab)?a:b;max=(temp>c)?temp:c; 课后答案网www.khdaw.comprintf("A,B,C中最大数是%d,",max);}5.5main(){intx,y;printf("输入x:");scanf("%d",&x);if(x<1){y=x;printf("X-%d,Y=X=%dn",x,y);}elseif(x<10){y=2*x-1;printf("X=%d,Y=2*X-1=%dn",x,y);}else{y=3*x-11;printf("X=5d,Y=3*x-11=%dn",x,y);}}5.7给一个不多于5位的正整数,要求:1.求它是几位数2.分别打印出每一位数字3.按逆序打印出各位数字.例如原数为321,应输出123.main(){longintnum;intindiv,ten,hundred,housand,tenthousand,place;printf("请输入一个整数(0-99999):");scanf("%ld",&num);if(num>9999)place=5;elseif(num>999)place=4;elseif(num>99)place=3;elseif(num>9)place=2;elseplace=1;printf("place=%dn",place);printf("每位数字为:");ten_thousand=num/10000;thousand=(num-tenthousand*10000)/1000;hundred=(num-tenthousand*10000-thousand*1000)/100;ten=(num-tenthousand*10000-thousand*1000-hundred*100)/10;indiv=num-tenthousand*10000-thousand*1000-hundred*100-ten*10;switch(place) 课后答案网www.khdaw.com{case5:printf("%d,%d,%d,%d,%d",tenthousand,thousand,hundred,ten,indiv);printf("n反序数字为:");printf("%d%d%d%d%dn",indiv,ten,hundred,thousand,tenthousand);break;case4:printf("%d,%d,%d,%d",thousand,hundred,ten,indiv);printf("n反序数字为:");printf("%d%d%d%dn",indiv,ten,hundred,thousand);break;case3:printf("%d,%d,%dn",hundred,ten,indiv);printf("n反序数字为:");printf("%d%d%dn",indiv,ten,hundred);case2:printf("%d,%dn",ten,indiv);printf("n反序数字为:");printf("%d%dn",indiv,ten);case1:printf("%dn",indiv);printf("n反序数字为:");printf("%dn",indiv);}}5.81.if语句main(){longi;floatbonus,bon1,bon2,bon4,bon6,bon10;/*初始化变量*/bon1=100000*0.1;bon2=100000*0.075+bon1;bon4=200000*0.05+bon2;bon6=200000*0.03+bon4;bon10=400000*0.015+bon6;printf("请输入利润");scanf("%ld",&i);/*计算*/if(i<=le5)bonus=i*0.1;elseif(i<2e5)bonus=bon1+(1-100000)*0.075;elseif(i<=4e5)bonus=bon2+(i-200000)*0.05;elseif(i<=6e5)bonus=bon4+(i-400000)*0.03;elseif(i<=le6)bonus=bon6+(i-600000)*0.015;elsebonus=bon10+(i-1000000)*0.01; 课后答案网www.khdaw.comprintf("奖金是%10.2f",bonus);}用switch语句编程序main(){longi;floatbonus,bon1,bon2,bon4,bon6,bon10;intbranch;/*初始化变量*/bon1=100000*0.1;bon2=bon1+100000*0.075bon4=bon2+200000*0.05;bon6=bon4+200000*0.03;bon10=bon6+400000*0.015;printf("请输入利润:");scanf("%ld",&i);branch=i/100000;if(branch>10)branch=10;/*计算*/switch(branch){case0:bonus=i*0.1;break;case1:bonus=bon1+(i-100000)*0.075;break;case2:case3:bonus=bon2+(i-200000)*0.05;break;case4:case5:bonus=bon4+(i-400000)*0.03;break;case6:case7:case8:case9:bonus=bon6+(i-600000)*0.015;break;case10:bonus=bon10+(i-1000000)*0.01;}printf("奖金是%10.2f",bonus);}5.9输入四个整数,按大小顺序输出.main(){intt,a,b,c,d;printf("请输入四个数:");scanf("%d,%d,%d,%d",&a,&b,&c,&d);printf("nna=%d,b=%d,c=%d,d=%dn",a,b,c,d);if(a>b){t=a;a=b;b=t;}if(a>c){t=a;a=c;c=t;} 课后答案网www.khdaw.comif(a>d){t=a;a=d;d=t;}if(b>c){t=b;b=c;c=t;}if(b>d){t=b;b=d;d=t;}if(c>d){t=c;c=d;d=t;}printf("n排序结果如下:n");printf("%d%d%d%dn",a,b,c,d);}5.10塔main(){inth=10;floatx,y,x0=2,y0=2,d1,d2,d3,d4;printf("请输入一个点(x,y):");scanf("%f,%f",&x,&y);d1=(x-x0)*(x-x0)+(y-y0)(y-y0);d2=(x-x0)*(x-x0)+(y+y0)(y+y0);d3=(x+x0)*(x+x0)+(y-y0)*(y-y0);d4=(x+x0)*(x+x0)+(y+y0)*(y+y0);if(d1>1&&d2>1&&d3>1&&d4>1)h=0;printf("该点高度为%d",h);}第六章循环语句6.1输入两个正数,求最大公约数最小公倍数.main(){inta,b,num1,num2,temp;printf("请输入两个正整数:n");scanf("%d,%d",&num1,&num2);if(num1main(){charc;intletters=0,space=0,degit=0,other=0;printf("请输入一行字符:n");while((c=getchar())!="n"){if(c>="a"&&c<="z"||c>"A"&&c<="Z")letters++;elseif(c=="")space++;elseif(c>="0"&&c<="9")digit++;elseother++;}printf("其中:字母数=%d空格数=%d数字数=%d其它字符数=%dn",letters,space,digit,other);}6.3求s(n)=a+aa+aaa+…+aa…a之值,其中工是一个数字.解:main(){inta,n,count=1,sn=0,tn=0;printf("请输入a和n的值:n");scanf("%d,%d",&a,&n);printf("a=%dn=%dn",a,n);while(count<=n){tn=tn+a;sn=sn+tn;a=a*10;++count;}printf("a+aa+aaa+…=%dn",sn);} 课后答案网www.khdaw.com6.4求1+2!+3!+4!+…+20!.main(){floatn,s=0,t=1;for(n=1;n<=20;n++){t=t*n;s=s+t;}printf("1!+2!+…+20!=%en",s);}6.5main(){intN1=100,N2=50,N3=10;floatk;floats1=0,s2=0,s3=0;for(k=1;k<=N1;k++)/*计算1到100的和*/{s1=s1+k;}for(k=1;k<=N2;k++)/*计算1到50各数平方和*/{s2=s2+k*k;}for(k=1;k<=N3;k++){s3=s3+1/k;}printf("总和=%8.2fn",s1+s2+s3);}6.6水仙开花main(){inti,j,k,n;printf(""水仙花"数是:");for(n=100;n<1000;n++){i=n/100;j=n/10-i*10;k=n%10;if(i*100+j*10+k==i*i*i+j*j*j+k*k*k){printf("%d",n);} 课后答案网www.khdaw.com}printf("n");}6.7完数main()#includeM1000/*定义寻找范围*/main(){intk0,k1,k2,k3,k4,k5,k6,k7,k8,k9;inti,j,n,s;for(j=2;j<=M;j++){n=0;s=j;for(i=1;i1)printf("%d,%d",k0,k1);if(n>2)printf(",%d",k2);if(n>3)printf(",%d",k3);if(n>4)printf(",%d",k4);if(n>5)printf(",%d",k5);if(n>6)printf(",%d",k6);if(n>7)printf(",%d",k7);if(n>8)printf(",%d",k8);if(n>9)printf(",%d",k9);printf("n");}}方法二:此题用数组方法更为简单.main(){staticintk[10];inti,j,n,s;for(j=2;j<=1000;j++){ 课后答案网www.khdaw.comn=-1;s=j;for(i=1;i0){x1=(x2+1)*2;x2=x1;day--;}printf("桃子总数=%dn",x1);}6.14打印图案main(){inti,j,k;for(i=0;i<=3;i++){for(j=0;j<=2-1;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");}6.15乒乓比赛main(){chari,j,k;/*i是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++){ 课后答案网www.khdaw.comif(i!=k&&j!=k){if(i!="x"&&k!="x"&&k!="z")printf("顺序为:na-%ctb--%ctc--%cn",i,j,k);}}}} 课后答案网www.khdaw.comC语言设计谭浩强第三版的课后习题答案(二)PostBy:2008-3-1220:42:277.1用筛选法求100之内的素数./*用筛选法求100之内的素数*/#include#defineN101main(){inti,j,line,a[N];for(i=2;ia[j])min=j; 课后答案网www.khdaw.comtemp=a[i];a[i]=a[min];a[min]=temp;}/*输出*/printf("n排序结果如下:n");for(i=0;iend)a[10]=number;else{for(i=0;i<10;i++){if(a[i]>number){temp1=a[i];a[i]=number;for(j=i+1;j<11;j++){temp2=a[j]; 课后答案网www.khdaw.coma[j]=temp1;temp1=temp2;}break;}}}for(i=0;j<11;i++)printf("a%6d",a[i]);}7.5将一个数组逆序存放。/*数组逆序存放*/#defineN5main(){inta[N]={8,6,5,4,1},i,temp;printf("n初始数组:n");for(i=0;imax){max=a[i][j];maxj=j;}for(k=0,flag1=1;ka[k][max])flag1=0;if(flag1){printf("n第%d行,第%d列的%d是鞍点n",i,maxj,max);flag2=1;}}if(!flag2)printf("n矩阵中无鞍点!n");}7.9变量说明:top,bott:查找区间两端点的下标;loca:查找成功与否的开关变量./*折半查找*/ 课后答案网www.khdaw.com#include#defineN15main(){inti,j,number,top,bott,min,loca,a[N],flag;charc;printf("输入15个数(a[i]>[i-1])n);scanf("%d",&a[0]);i=1;while(i=a[i-1])i++;esle{printf("请重输入a[i]");printf("必须大于%dn",a[i-1]);}}printf("n");for(i=0;ia[N-1]))loca=-1;while((loca==0)&&(top<=bott)){min=(bott+top)/2;if(number==a[min]){loca=min;printf("%d位于表中第%d个数n",number,loca+1);}elseif(number="A"&&text[i][j]<="Z")uppn+=1;elseif(text[i][j]>="a"&&text[i][j]<="z")lown+=1;elseif(text[i][j]>="1"&&text[i][j]<="9")dign+=1;elseif(text[i][j]="")span+=1;elseothn+=1;}}for(i=0;i<3;i++)printf("%s=n",text[i]);printf("大写字母数:%dn",uppn);printf("小写字母数:%dn",lown);printf("数字个数:%dn",dign);printf("空格个数:%dn",span);printf("其它字符:%dn",othn);}7.11/*打印图案*/main(){staticchara[5]={"*","*","*","*","*"};inti,j,k;charspace="";for(i=0;i<=5;i++){printf("n");for(j=1;j<=3*i;j++) 课后答案网www.khdaw.comprintf("%lc",space);for(k=0;k<=5;k++)printf("%3c",a[k];}}7.12/*译电文*/#includemain(){inti,n;charch[80],tran[80];printf("请输入字符:");gets(ch);printf("n密码是%c",ch);i=0;while(ch[i]!=""){if((ch[i]>="A")&&(ch[i]<="Z"))tran[i]=26+64-ch[i]+1+64;elseif((ch[i]>="a")&&(ch[i]<="z"))tran[i]=26+96-ch[i]+1+96;elsetran[i]=ch[i];i++;}n=i;printf("n原文是:");for(i=0;imain(){inti,resu;chars1[100],s2[100];printf("请输入字符串1:n");gets(s1);printf("n请输入字符串2:n");gets(s2);i=0;while((s1[i]==s2[i])&&(s1[i]!=""))i++;if(s1[i]==""&&s2[i]=="")resu=0;elseresu=s1[i]-s2[i];printf("%s与%s比较结果是%d",s1,s2,resu);}7.15/*字符串复制*/#includemain(){charfrom[80],to[80];inti;printf("请输入字符串");scanf("%s",from);for(i=0;i<=strlen(from);i++)to[i]=from[i];printf("复制字符串为:%sn",to);}第八章函数8.1(最小公倍数=u*v/最大公约数.)hcf(u,v)intu,v;(inta,b,t,r;if(u>v){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)intu,v,h;{intu,v,h,l; 课后答案网www.khdaw.comscanf("%d,%d",&u,&v);h=hcf(u,v);printf("H.C.F=%dn",h);l=lcd(u,v,h);printf("L.C.d=%dn",l);}{return(u*v/h);}main(){intu,v,h,l;scanf("%d,%d",&u,&v);h=hcf(u,v);printf("H.C.F=%dn",h);l=lcd(u,v,h);printf("L.C.D=%dn",l);}8.2求方程根#includefloatx1,x2,disc,p,q;greater_than_zero(a,b)floata,b;{x1=(-b+sqrt(disc))/(2*a);x2=(-b-sqrt(disc))/(2*a);}equal_to_zero(a,b)floata,b;{x1=x2=(-b)/(2*a);}smaller_than_zero(a,b)floata,b;{p=-b/(2*a);'

您可能关注的文档