C语言程序设计第三版谭浩强课后习题答案完整版
谭浩强C程序设计第三版课后答案(整本书)
C程序设计(第三版)课后习题参考解答写一个程序,输入a,b,c三个值,输出其中最大者。
解:main ( ){int a,b,c,max;printf(“请输入三个数a,b,c: \n”);scanf(“%d,%d,%d”,&a,&b,&c);max=a;if (max<b)max=b;if (max<c)max=c;printf(“最大数为:%d ”,max);}第2章程序的灵魂——算法什么叫结构化的算法为什么要提倡结构化的算法解:由一些基本结构顺序组成的算法称为结构化的算法。
由于在基本结构之间不存在非顺序的跳转,流程的转移只存在于一个基本结构范围之内,因而提高了算法的质量。
什么叫结构化程序设计它的主要内容是什么解:结构化程序就是用高级语言表示的结构化算法。
它的主要内容包括“自顶向下,逐步细化”的分析方法和“模块化设计”的解决方法,以及“结构化编码”的实现方法。
第3章数据类型、运算符与表达式3.3请将下面各数用八进制和十六进制数表示:(1)10 (2)32 (3)75 (4)-617(5)-111 (6)2483 (7)-28654 (8)21003解:(1) (10) 10=(12) 8=(A) 16(2) (32)10=(40)8=(20) 16(3)(75)10=(113)8=(4B) 16(4)(-617)10=(176627)8=(FD97) 16此题可以这样考虑:带符号数在计算机中采用补码表示,正数的补码与原码相同,负数的补码=模+真值。
若使用16位存储,模为216=65536。
-617的补码为65536+(-167)=64919=(176627)8=(FD97)16(5)(-111)10=(177621)8=(FF91) 16(6)(2483)10=(4663)8=(9B3) 16(7)(-28654)10=(110022)8=(9012) 16(8)(21003)10=(51013)8=(520B) 163.4将以下三各整数分别赋给不同类型的变量,请画出赋值后数据在内存中的存储形式。
C语言程序设计第三版谭浩强课后习题答案完整版
C语言程序设计第三版谭浩强课后习题答案完整版第一章1.5请参照本章例题,编写一个C程序,输出以下信息:**************************解:”););}1.6输出其中最大值。
解:mian(){int a,b,c,max;printf(“请输入三个数a,b,c:\n”);scanf(“%d,%d,%d”,&a,&b,&c); max=a;if(max<b)max=b;if(max<c)max=c;printf(“最大数为:“%d”,max);\101abc AㄩN3.7 要将"China"译成密码,译码规律是:用原来字母后面的第4个字母代替原来的字母.例如,字母"A"后面第4个字母是"E"."E"代替"A"。
因此,"China"应译为"Glmre"。
请编一程序,用赋初值的方法使cl、c2、c3、c4、c5五个变量的值分别为,’C’、’h’、’i’、’n’、’a’,经过运算,使c1、c2、c3、c4、c5分别变为’G’、’l’、’m’、’r’、’e’,并输出。
解:#include <stdio.h>{是}密码是Glmre3.9求下面算术表达式的值。
(1)x+a%3*(int)(x+y)%2/4设x=2.5,a=7,y=4.7(2)(float)(a+b)/2+(int)x%(int)y 设a=2,b=3,x=3.5,y=2.5 (1)2.5(2)3.53.10写出程序运行的结果。
main(){int i,j,m,n;i=8;(4)解:(1) 24 (2) 10 (3) 60 (4) 0 (5) 0 (6) 0第四章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’。
C语言程序的设计第三版谭浩强课后习题答案完整版
1.5#include <stdio.h>void main(){printf("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n");printf(" Very good! \n");printf("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n");}1.6#include <stdio.h>void main(){float max(float x,float y,float z);float a,b,c;printf("请分别输入a,b,c:\n");scanf("%f,%f,%f",&a,&b,&c);printf("max=%f",max(a,b,c));}float max(float x,float y,float z){float m,n;m=x>y? x: y;n=m>z? m: z;return(n);}3.6#include <stdio.h>void main(){char c1=’a’,c2=’b’,c3=’c’,c4=’\101’,c5=’\116’;printf(“a%c b%c\tc%c\tabc\n”,c1,c2,c3);printf(“\t\b%c %c\n”,c4,c5);}3.9.1#include <stdio.h>void main(){double x=2.5,y=4.7,z;int a=7;z=x+a%3*(int)(x+y)%2/4;printf("该表达式的值为:%f",z);}3.9.2#include <stdio.h>void main(){int a=2,b=3;float x=3.5,y=2.5,z;z=(float)(a+b)/2+(int)x%(int)y;printf("该表达式的值为:%f",z);}4.5#include <stdio.h>void main(){int a=5,b=7;double x=67.8564,y=-789.124;char c='A';long n=1234567;unsigned u=65535;printf("%d%d\n",a,b);printf("%3d%3d\n",a,b);printf("%f,%f\n",x,y);printf("%-10f,%-10f\n",x,y);printf("%8.2f,%8.2f,%.4f,%.4f,%3f,%3f\n",x,y,x,y,x,y);printf("%e,%10.2e\n",x,y);printf("%c,%d,%o,%x\n",n,n,n);printf("%ld,%lo,%x\n",n,n,n);printf("%u,%o,%x,%d\n",u,u,u,u);printf("%s,%5.3s\n","COMPUTER","COMPUTER");}4.6#include <stdio.h>void 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);}4.7#include<stdio.h>void main(){int a,b;float x,y,z;char c1,c2;scanf("%5d%5d",&a,&b); scanf("%f%f%*f%f",&x,&y,&z); scanf("%c%c",&c1,&c2);}4.8#include <stdio.h>#define pi 3.14void main(){float r,h,l,s1,s2,v1,v2;printf("请输入r,h的值:\n"); scanf("%f,%f",&r,&h);l=2*pi*r;s1=pi*r*r;s2=4*s1;v1=4/3*pi*r*r*r;v2=s1*h;printf("圆周长为:%.2f\n",l); printf("圆面积为:%.2f\n",s1); printf("圆球表面积为:%.2f\n",s2); printf("圆球体积为:%.2f\n",v1); printf("圆柱体积为:%.2f\n",v2); }5.5 #include <stdio.h>void main(){float x,y;printf("请输入x的值:\n");scanf("%f",&x);if(x<1)y=x;elseif(x>=1&&x<10)y=2*x-1;elsey=3*x-11;printf("x与y的值分别为:\n%f,%f",x,y); printf("\n");}5.6 #include <stdio.h>void main(){float b;int a;printf("请输入该同学的成绩:\n");scanf("%f",&b);while (b<0||b>100){printf("输入有误,请重新输入:\n");scanf("%f",&b);}a= int(b/10);if(a<6) a=5;switch(a){case 10:case 9: printf("该同学为等级:A");break;case 8: printf("该同学为等级:B");break;case 7: printf("该同学为等级:C");break;case 6: printf("该同学为等级:D");break;case 5: printf("该同学为等级:E");break;}}#include <stdio.h>#include <math.h>void main(){int x,y,m,n,i=1,j=0;printf("请给出一个不多于5位的正整数:\n");scanf("%d",&x);while(x<=0||x>=100000){ printf("输入有误\n请给出一个不多于5位的正整数:\n");scanf("%d",&x);}for(n=0;n<5;n++){y=int(x/pow(10,n));if(y>0&&y<10)m=n+1;}printf("它是%d位数\n",m);printf("从高到低位依次为\n");do{printf(" %d",int(x/pow(10,m-i))%10);i++;}while(m-i>=0);printf("\n逆序输出为\n");do{printf(" %d",int(x/pow(10,j))%10);j++;}while(j-m<0);printf("\n");}6.1输入两个正整数m和n,求其最大公约数和最小公倍数。
C语言程序设计第三版谭浩强课后习题答案完整版
C语言程序设计第三版谭浩强课后习题答案完整版第一章1.5请参照本章例题,编写一个C程序,输出以下信息:**************************Very Good!**************************解:mian(){printf(“**************************”);printf(“\n”);printf(“Very Good!\n”);printf(“\n”);printf(“**************************”);}1.6 编写一个程序,输入a、b、c三个值,输出其中最大值。
解:mian(){int a,b,c,max;printf(“请输入三个数a,b,c:\n”);scanf(“%d,%d,%d”,&a,&b,&c);max=a;if(max<b)max=b;if(max<c)max=c;printf(“最大数为:“%d”,max);}第三章3.6写出以下程序运行的结果。
main(){char c1=’a’,c2=’b’,c3=’c’,c4=’\101’,c5=’\116’;printf(“a%cb%c\tc%c\tabc\n”,c1,c2,c3);printf(“\t\b%c %c”,c4,c5);}解:aaㄩbbㄩㄩㄩccㄩㄩㄩㄩㄩㄩabcAㄩN3.7要将"China"译成密码,译码规律是:用原来字母后面的第4个字母代替原来的字母.例如,字母"A"后面第4个字母是"E"."E"代替"A"。
因此,"China"应译为"Glmre"。
请编一程序,用赋初值的方法使cl、c2、c3、c4、c5五个变量的值分别为,’C’、’h’、’i’、’n’、’a’,经过运算,使c1、c2、c3、c4、c5分别变为’G’、’l’、’m’、’r’、’e’,并输出。
谭浩强C语言程序设计教程(第三版)课后习题答案
for(i=1;i<=n;i++)
{
t=t*i; /* 实现阶乘*/
sum=sum+1/t;
}
printf("e=%lf",sum);
}
源程序二:(2)用while循环实现
#include<math.h>
}
——————————————————————————————————————
5.19 编写程序,输出从公元1000年至2000年所有闰年的年号 每输出3个年号换一行。
判断公元年是否闰年的条件是:
(1)公元年数如能被4整除,而不能被100整除,则是闰年。
(2)公元年数能被400整除也是闰年。
if(d1<d0)m1=m1-1;
if(m1<m0)y1=y1-1;
age=y1-y0;
printf("\nAge=%d",age);
}
——————————————————————————————————————
4.23 编写程序,输入一个整数,打印出它是奇数还是偶数。
3.33 编写程序,读入三个整数给a,b,c,然后交换它们中的数,把a中原来的值给b,
把b中原来的值给c,把c中的值给a。
※程序如下※
main( )
{
int a,b,c,t=0;
printf("input a b c:\n");
scanf("%d%d%d",&a,&b,&c);
else if(x==0)
C语言程序设计第三版谭浩强课后习题答案完整版.doc
C语言程序设计第三版谭浩强课后习题答案完整版第一章请参照本章例题,编写一个C程序,输出以下信息:**************************Very Good!**************************解:mian(){printf( “**************************”);printf( “n”);printf( “Very Good!n”);printf( “n”);printf( “**************************”);}编写一个程序,输入a、 b、 c 三个值,输出其中最大值。
解:mian(){int a,b,c,max;printf(“请输入三个数a,b,c:\n” );scanf( “%d,%d,%d”,&a,&b,&c);max=a;if(max<b)max=b;if(max<c)max=c;printf(“最大数为:“ %d” ,max);}第三章写出以下程序运行的结果。
main(){charc1=’a’,c2= ’b’,c3= ’c’,c4= ’101’,c5= ’116’;printf( “a%cb%c n”,c1,c2,c3);printf( “b%c %c”,c4,c5);}解:aa ㄩ bb ㄩㄩㄩ cc ㄩㄩㄩㄩㄩㄩabcAㄩ N要将 "China" 译成密码,译码规律是:用原来字母后面的第 4 个字母代替原来的字母.例如,字母"A" 后面第 4 个字母是 "E" ."E" 代替 "A" 。
因此, "China" 应译为 "Glmre" 。
请编一程序,用赋初值的方法使cl 、 c2、 c3、c4、c5 五个变量的值分别为,’ C’、’ h’、’i ’、’n’、’ a’,经过运算,使c1、c2、 c3、c4、c5 分别变为’ G’、’l ’、’m’、’r ’、’ e’,并输出。
谭浩强《C程序设计》第三版习题答案(全)
谭浩强《C程序设计》第三版习题答案(全)第一章1.5#include <stdio.h>void main(){printf("* * * * * * * * *\n Very good!\n* * * * * * * * *\n");}1.6(法一)#include <stdio.h>main(){int a,b,c,max;scanf("%d,%d,%d",&a,&b,&c);max=(a>b)?a:b;max=(max>c)?max:c;printf("max=%d\n",max);}(法二)main(){int a,b,c,max;scanf("%d,%d,%d",&a,&b,&c);if(a>b&&a>c)max=a;else if (b>c)max=b;elsemax=c;printf("max=%d\n",max);}(法三)# include <stdio.h>void main(){int max(int x,int y,int z);int a,b,c,d;scanf("%d,%d,%d",&a,&b,&c);d=max(a,b,c);printf("max=%d\n",d);}int max(int x,int y,int z){int A,B;if(x>y) A=x;else A=y;if(z>A) B=z;else B=A;return(B);}第三章3.3(1)(10)10=(12)8=(a)16(2)(32)10=(40)8=(20)16(3)(75)10=(113)8=(4b)16(4)(-617)10=(176627)8=(fd97)16(5)(-111)10=(177621)8=(ff91)16(6)(2483)10=(4663)8=(963)16(7)(-28654)10=(110022)8=(9012)16(8)(21003)10=(51013)8=(520b)163.6aabb (8)cc (8)abc (7)AN3.7main(){char c1='C',c2='h',c3='i',c4='n',c5='a';c1+=4, c2+=4, c3+=4, c4+=4, c5+=4; printf("%c%c%c%c%c\n",c1,c2,c3,c4,c5); }3.8main(){int c1,c2;c1=97;c2=98;printf("%c %c",c1,c2);}3.9(2)=3.53.109,11,9,103.12(1)24 (2)10 (3)60 (4)0 (5)0 (6)0第四章4.4main(){int a,b,c;long int u,n;float x,y,z;char c1,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=%2d b=%2d c=%2d\n",a,b,c);printf("x=%8.6f,y=%8.6f,z=%9.6f\n",x,y,z);printf("x+y=%5.2f y+z=%5.2f z+x=%5.2f\n",x+y,y+z,z+x); printf("u=%6ld n=%9ld\n",u,n);printf("c1='%c'or %d(ASCII)\n",c1,c1);printf("c2='%c'or %d(ASCII)\n",c2,c2);}4.5575 767.856400,-789.12396267.856400,-789.12396267.86 -789.12,67.856400,-789.123962,67.856400,-789.123962 6.785640e+001,-7.89e+002A,65,101,411234567,4553207,d68765535,177777,ffff,-1COMPUTER, COM4.6x=8.5 y=71.82/c1=A c2=a/4.710 20Aa1.5 -3.75 +1.4,67.8/(空3)10(空3)20Aa1.5(空1)-3.75(空1)(随意输入一个数),67.8回车4.8main(){float pi,h,r,l,s,sq,sv,sz;pi=3.1415926;printf("input r,h\n");scanf("%f,%f",&r,&h);l=2*pi*r;s=r*r*pi;sq=4*pi*r*r;sv=4.0/3.0*pi*r*r*r;sz=pi*r*r*h;printf("l=%6.2f\n",l);printf("s=%6.2f\n",s);printf("sq=%6.2f\n",sq);printf("vq=%6.2f\n",sv);printf("vz=%6.2f\n",sz);}4.9main(){float c,f;scanf("%f",&f);c=(5.0/9.0)*(f-32);printf("c=%5.2f\n",c);}4.10#include"stdio.h"main(){char c1,c2;scanf("%c,%c",&c1,&c2);putchar(c1);putchar(c2);printf("\n");printf("%c%c\n",c1,c2);}第五章5.3(1)0 (2)1 (3)1 (4)0 (5)15.4main(){int a,b,c;scanf("%d,%d,%d",&a,&b,&c); if(a<b)if(b<c)printf("max=%d\n",c);elseprintf("max=%d\n",b); else if(a<c)printf("max=%d\n",c); elseprintf("max=%d\n",a);}main(){int a,b,c,temp,max;scanf("%d,%d,%d",&a,&b,&c); temp=(a>b)?a:b;max=(c>temp)?c:temp;printf("max=%d",max);}5.5main(){int x,y;scanf("%d",&x);if(x<1)y=x;else if(x<10)y=2*x-1;else y=3*x-11;printf("y=%d",y);}5.6main(){int score,temp,logic;char grade;logic=1;while(logic){scanf("%d",&score);if(score>=0&&score<=100)logic=0;}if(score==100)temp=9;elsetemp=(score-score%10)/10;switch(temp){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"score=%d,grade=%c",score,grade);}5.7main(){long int num;int indiv,ten,hundred,thousand,ten_thousand,place;scanf("%ld",&num);if(num>9999) place=5;else if(num>999) place=4;else if(num>99) place=3;else if(num>9) place=2;else place=1;printf("place=%d\n",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;switch(place){case 5:printf("%d,%d,%d,%d,%d\n",ten_thousand,thousand,hundred,ten,indiv);printf("%d,%d,%d,%d,%d\n",indiv,ten,hundred,thousand,ten_thousand);break;case 4:printf("%d,%d,%d,%d\n",thousand,hundred,ten,indiv);printf("%d,%d,%d,%d\n",indiv,ten,hundred,thousand);break;case 3:printf("%d,%d,%d\n",hundred,ten,indiv);printf("%d,%d,%d\n",indiv,ten,hundred);break;case 2:printf("%d,%d\n",ten,indiv);printf("%d,%d\n",indiv,ten);break;case 1:printf("%d\n",indiv);printf("%d\n",indiv);}}5.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",&i);if(i<=1e5)bonus=i*0.1;else if(i<=2e5)bonus=bon1+(i-100000)*0.075;else if(i<=4e5)bonus=bon2+(i-200000)*0.05;else if(i<=6e5)bonus=bon4+(i-400000)*0.03;else if(i<=1e6)bonus=bon6+(i-600000)*0.015;else bonus=bon10+(i-1000000)*0.01;printf("bonus=%10.2f",bonus);}main(){long i;float 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;scanf("%ld",&i);branch=i/100000;if(branch>10)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;}printf("bonus=%10.2f",bonus);}4.9main(){int t,a,b,c,d;scanf("%d,%d,%d,%d",&a,&b,&c,&d);if(a>b){t=a;a=b;b=t;}if(a>c){t=a;a=c;c=t;}if(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("%d %d %d %d\n",a,b,c,d);}5.10main(){int h=10;float x,y,x0=2,y0=2,d1,d2,d3,d4;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("h=%d",h);}第六章6.1main(){int a,b,num1,num2,temp;scanf("%d,%d",&num1,&num2);if(num1<num2){temp=num1;num1=num2;num2=temp;}a=num1;b=num2;while(b!=0){temp=a%b;a=b;b=temp;}printf("%d\n",a);printf("%d\n",num1*num2/a);}6.2#include"stdio.h"main(){char c;int letter=0,space=0,digit=0,other=0;while((c=getchar())!='\n'){if(c>='a'&&c<='z'||c>='A'&&c<='Z') letter++;else if(c==' ')space++;else if(c>='0'&&c<='9')digit++;else other++;}printf("letter=%d\nspace=%d\ndigit=%d\nother=%d\n",letter,space,digit,other); }6.3main(){int a,n,count=1,sn=0,tn=0;scanf("%d,%d",&a,&n);while(count<=n){tn+=a;sn+=tn;a*=10;++count;}printf("a+aa+aaa+...=%d\n",sn);}6.4main(){float n,s=0,t=1;for(n=1;n<=20;n++){t*=n;s+=t;}printf("s=%e\n",s);}6.5main(){int N1=100,N2=50,N3=10;float k;float s1=0,s2=0,s3=0;for(k=1;k<=N1;k++)s1+=k;for(k=1;k<=N2;k++)s2+=k*k;for(k=1;k<=N3;k++)s3+=1/k;printf("s=%8.2f\n",s1+s2+s3);}6.6main(){int i,j,k,n;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("n=%d\n",n);}}6.7#define M 1000main(){int k0,k1,k2,k3,k4,k5,k6,k7,k8,k9;int i,j,n,s;for(j=2;j<=M;j++){n=0;s=j;for(i=1;i<j;i++){if((j%i)==0){n++;s=s-i;switch(n){case 1:k0=i;break;case 2:k1=i;break;case 3:k2=i;break;case 4:k3=i;break;case 5:k4=i;break;case 6:k5=i;break;case 7:k6=i;break;case 8:k7=i;break;case 9:k8=i;break;case 10:k9=i;break;}}}if(s==0){printf("j=%d\n",j);if(n>1)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\n",k9);}}}main(){static int k[10];int i,j,n,s;for(j=2;j<=1000;j++){n=-1;s=j;for(i=1;i<j;i++){if((j%i)==0){n++;s=s-i;k[n]=i;}}if(s==0){printf("j=%d\n",j);for(i=0;i<n;i++)printf("%d,",k[i]);printf("%d\n",k[n]);}}6.8main(){int n,t,number=20; float a=2;b=1;s=0;for(n=1;n<=number;n++) {s=s+a/b;t=a,a=a+b,b=t;}printf("s=%9.6f\n",s);}6.9main(){float sn=100.0,hn=sn/2; int n;for(n=2;n<=10;n++){sn=sn+2*hn;hn=hn/2;}printf("sn=%f\n",sn); printf("hn=%f\n",hn);}6.10main(){int day,x1,x2;day=9;x2=1;while(day>0){x1=(x2+1)*2;x2=x1;day--;}printf("x1=%d\n",x1);}6.11#include"math.h"main(){float a,xn0,xn1;scanf("%f",&a);xn1=(xn0+a/xn0)/2;do{xn0=xn1;xn1=(xn0+a/xn0)/2;}while(fabs(xn0-xn1)>=1e-5);printf("a=%5.2f\n,xn1=%8.2f\n",a,xn1); }6.12#include"math.h"main(){float x,x0,f,f1;x=1.5;do{x0=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.2f\n",x);}6.13#include"math.h"main(){float x0,x1,x2,fx0,fx1,fx2;do{scanf("%f,%f",&x1,&x2);fx1=x1*((2*x1-4)*x1+3)-6;fx2=x2*((2*x2-4)*x2+3)-6;}while(fx1*fx2>0);do{x0=(x1+x2)/2;fx0=x0*((2*x0-4)*x0+3)-6;if((fx0*fx1)<0){x2=x0;fx2=fx0;}else{x1=x0;fx1=fx0;}}while(fabs(fx0)>=1e-5);printf("x0=%6.2f\n",x0);}6.14main(){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");}}6.15main(){char i,j,k;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("\na--%c\tb--%c\tc--%c\n",i,j,k);}}}}第七章7.1#include <math.h>#define N 101main(){ int i,j,line,a[N];for (i=2;i<N;i++) a[i]=i;for (i=2;i<sqrt(N);i++)for (j=i+1;j<N;j++){if(a[i]!=0 && a[j]!=0)if (a[j]%a[i]==0)a[j]=0; } printf("\n");for (i=2,line=0;i<N;i++){ if(a[i]!=0){ printf("%5d",a[i]);line++; }if(line==10){ printf("\n");line=0; }}}7.2#define N 10main(){int i,j,min,temp,a[N];for(i=0;i<N;i++)scanf("%d",&a[i]);for(i=0;i<N-1;i++){min=i;for(j=i+1;j<N;j++)if(a[min]>a[j])min=j;temp=a[i];a[i]=a[min];a[min]=temp;}for(i=0;i<N;i++)printf("%5d",a[i]);}7.3main(){float a[3][3],sum;int i,j;for(i=0;i<3;i++)for(j=0;j<3;j++){scanf("%f",&sum);a[i][j]=sum;}for(i=0;i<3;i++)sum=sum+a[i][i];printf("sum=%f",sum);}7.4main(){int a[11]={1,4,6,9,13,16,19,28,40,100}; int temp1,temp2,number, end,i,j;scanf("%d",&number);end=a[9];if(number>end) 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];a[j]=temp1;temp1=temp2;}break;}}}for(i=0;i<11;i++)printf("%6d",a[i]);}7.5#define N 5main(){int a[N]={8,6,5,4,1},i,temp;for(i=0;i<N/2;i++){temp=a[i];a[i]=a[N-i-1];a[N-i-1]=temp;}for(i=0;i<N;i++)printf("%4d",a[i]);}7.6#define N 11main(){int i,j,a[N][N];for(i=1;i<N;i++){a[i][i]=1;a[i][1]=1;}for(i=3;i<N;i++)for(j=2;j<i;j++)a[i][j]=a[i-1][j-1]+a[i-1][j];for(i=1;i<N;i++){for(j=1;j<=i;j++)printf("%6d",a[i][j]);printf("\n");}}7.7main(){int a[16][16],i,j,k,p,m,n;p=1;while(p==1){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++)a[i][j]=0;j=n/2+1;a[1][j]=1;for(k=2;k<=n*n;k++){i=i-1;j=j+1;if((i<1)&&(j>n)){i=i+2;j=j-1;}else{if(i<1)i=n;if(j>n)j=1;}if(a[i][j]==0)a[i][j]=k;else{i=i+2;j=j-1;a[i][j]=k;}}for(i=1;i<=n;i++){for(j=1;j<=n;j++)printf("%3d",a[i][j]);printf("\n");}}7.8#define N 10#define M 10main(){int i,j,k,m,n,flag1,flag2,a[N][M],max,maxi,maxj; scanf("%d,%d",&n,&m);for(i=0;i<n;i++)for(j=0;j<m;j++)scanf("%d",&a[i][j]);flag2=0;for(i=0;i<n;i++){max=a[i][0];for(j=0;j<m;j++)if(max<a[i][j]){max=a[i][j];maxj=j;}for(k=0,flag1=1;k<n&&flag1;k++)if(max>a[k][maxj])flag1=0;if(flag1){ printf("\na[%d][%d]=%d\n",i,maxj,max);flag2=1;}}if(!flag2) printf("NOT");}7.9#include<stdio.h>#define N 15main(){int i,j,number,top,bott,min,loca,a[N],flag;char c;for(i=0;i<=N;i++)scanf("%d",&a[i]);flag=1;while(flag){scanf("%d",&number);loca=0;top=0;bott=N-1;if((number<a[0])||(number>a[N-1]))loca=-1;while((loca==0)&&(top<=bott)){min=(bott+top)/2;if(number==a[min]){loca=min;printf("number=%d,loca=%d\n",number,loca+1);}else if(number<a[min])bott=min-1;elsetop=min+1;}if(loca==0||loca==-1)printf("%d not in table\n",number);printf("continue Y/N or y/n\n");c=getchar();if(c=='N'||c=='n')flag=0;}}7.10main(){int i,j,uppn,lown,dign,span,othn;char text[3][80];uppn=lown=dign=span=othn=0;for(i=0;i<3;i++){gets(text[i]);for(j=0;j<80&&text[i][j]!='\0';j++){if(text[i][j]>='A'&&text[i][j]<='Z')uppn++;else if(text[i][j]>='a'&&text[i][j]<='z')lown++;else if(text[i][j]>='0'&&text[i][j]<='9')dign++;else if(text[i][j]==' ')span++;elseothn++;}}for(i=0;i<3;i++)printf("%s\n",text[i]);printf("uppn=%d\n",uppn);printf("lown=%d\n",lown);printf("dign=%d\n",dign);printf("span=%d\n",span);printf("othn=%d\n",othn);}7.11main(){static char a[5]={'*','*','*','*','*'};int i,j,k;char space=' ';for(i=0;i<=5;i++){printf("\n");for(j=1;j<=3*i;j++)printf("%1c",space);for(k=0;k<=5;k++)printf("%3c",a[k]);}}7.12#include<stdio.h>main(){int i,n;char ch[80],tran[80];gets(ch);i=0;while(ch[i]!='\0'){if((ch[i]>='A')&&(ch[i]<='Z'))tran[i]=26+64-ch[i]+1+64;else if((ch[i]>='a')&&(ch[i]<='z'))tran[i]=26+96-ch[i]+1+96;elsetran[i]=ch[i];i++;}n=i;for(i=0;i<n;i++)putchar(tran[i]);}7.13main(){char s1[80],s2[40];int i=0,j=0;scanf("%s",s1);scanf("%s",s2);while(s1[i]!='\0')i++;while(s2[j]!='\0')s1[i++]=s2[j++];s1[i]='\0';printf("s=%s\n",s1);}7.14#include<stdio.h>main(){int i,resu;char s1[100],s2[100];gets(s1);gets(s2);i=0;while((s1[i]==s2[i])&&(s1[i]!='\0'))i++;if(s1[i]=='\0'&&s2[i]=='\0')resu=0;elseresu=s1[i]-s2[i];printf("s1=%s,s2=%s,resu=%d\n",s1,s2,resu); }7.15#include"stdio.h"main(){char from[80],to[80];;int i;scanf("%s",from);for(i=0;i<=strlen(from);i++) to[i]=from[i];printf("%s\n",to);}第八章8.1hcf(u,v)int u,v;{int a,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)int u,v,h;{return(u*v/h);}main(){int u,v,h,l;scanf("%d,%d",&u,&v);h=hcf(u,v);printf("H.C.F=%d\n",h);l=lcd(u,v,h);printf("L.C.D=%d\n",l);}8.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);q=sqrt(-disc)/(2*a);}main(){float a,b,c;scanf("%f,%f,%f",&a,&b,&c);disc=b*b-4*a*c;if(fabs(disc)<=1e-5){equal_to_zero(a,b);printf("x1=%5.2f\tx2=%5.2f\n",x1,x2);}else if(disc>0){greater_than_zero(a,b);printf("x1=%5.2f\tx2=%5.2f\n",x1,x2);}else{smaller_than_zero(a,b);printf("x1=%5.2f+%5.2fi\tx2=%5.2f-%5.2fi\n",p,q,p,q);}}8.3main(){int number;scanf("%d",&number);if(prime(number))printf("yes");elseprintf("no");}int prime(number)int number;{int flag=1,n;for(n=2;n<number/2&&flag==1;n++)if(number%n==0)flag=0;return(flag);}8.4#define N 3int array[N][N];convert(array)int array[3][3];{int i,j,t;for(i=0;i<N;i++)for(j=i+1;j<N;j++){t=array[i][j];array[i][j]=array[j][i];array[j][i]=t;}}main(){int i,j;for(i=0;i<N;i++)for(j=0;j<N;j++)scanf("%d",&array[i][j]);convert(array);for(i=0;i<N;i++){printf("\n");for(j=0;j<N;j++)printf("%5d",array[i][j]);}}8.5main(){char str[100];scanf("%s",str);inverse(str);printf("%s\n",str);}inverse(str)char str[];{char t;int i,j;for(i=0,j=strlen(str);i<strlen(str)/2;i++,j--) {t=str[i];str[i]=str[j-1];str[j-1]=t;}}8.6char concate(str1,str2,str)char str1[],str2[],str[];{int i,j;for(i=0;str1[i]!='\0';i++)str[i]=str1[i];for(j=0;str2[j]!='\0';j++)str[i+j]=str2[j];str[i+j]='\0';}main(){char s1[100],s2[100],s[100];scanf("%s",s1);scanf("%s",s2);concate(s1,s2,s);printf("\ns=%s",s);}8.7main(){char str[80],c[80];void cpy();gets(str);cpy(str,c);printf("\n%s\n",c);}void cpy(s,c)char s[],c[];{int i,j;for(i=0,j=0;s[i]!='\0';i++)if(s[i]=='a'||s[i]=='A'||s[i]=='e'||s[i]=='E'||s[i]=='i'|| s[i]=='I'||s[i]=='o'||s[i]=='O'||s[i]=='u'||s[i]=='U'){c[j]=s[i];j++;}c[j]='\0';}8.8main(){char str[80];scanf("%s",str);insert(str);}insert(str)char str[];{int i;for(i=strlen(str);i>0;i--){str[i*2]=str[i];str[i*2-1]=' ';}printf("%s\n",str);}8.9int alph,digit,space,others;main(){char text[80];gets(text);alph=0,digit=0,space=0,others=0;count(text);printf("\nalph=%d,digit=%d,space=%d,others=%d\n",alph,digit,space,others); }count(str)char str[];{int i;for(i=0;str[i]!='\0';i++)if((str[i]>='a'&&str[i]<='z')||(str[i]>='A'&&str[i]<='Z'))alph++;else if(str[i]>='0'&&str[i]<='9')digit++;else if(strcmp(str[i],' ')==0)space++;elseothers++;}8.10int alph(c)char c;{if((c>='a'&&c<='z')||(c>='A'&&c<='Z'))return(1);elsereturn(0);}int longest(string)char string[];{int len=0,i,length=0,flag=1,place,point;for(i=0;i<=strlen(string);i++)if(alph(string[i]))if(flag){point=i;flag=0;}elselen++;else{flag=1;if(len>length){length=len;place=point;len=0;}}return(place);}main(){int i;char line[100];gets(line);for(i=longest(line);alph(line[i]);i++) printf("%c",line[i]);printf("\n");}8.11#define N 10char str[N];main(){int i,flag;for(flag=1;flag==1;){scanf("%s",str);if(strlen(str)>N)printf("input error");elseflag=0;}sort(str);for(i=0;i<N;i++)printf("%c",str[i]);}sort(str)char str[N];{int i,j;char t;for(j=1;j<N;j++)for(i=0;(i<N-j)&&(str[i]!='\0');i++)if(str[i]>str[i+1]){t=str[i];str[i]=str[i+1];str[i+1]=t;}}8.12#include<math.h>float solut(a,b,c,d)float a,b,c,d;{float x=1,x0,f,f1;do{x0=x;f=((a*x0+b)*x0+c)*x0+d;f1=(3*a*x0+2*b)*x0+c;x=x0-f/f1;}while(fabs(x-x0)>=1e-5);return(x);}main(){float a,b,c,d;scanf("%f,%f,%f,%f",&a,&b,&c,&d);printf("x=%10.7f\n",solut(a,b,c,d));}8.13main(){int x,n;float p();scanf("%d,%d",&n,&x);printf("P%d(%d)=%10.2f\n",n,x,p(n,x));}float p(tn,tx)int tn,tx;{if(tn==0)return(1);else if(tn==1)return(tx);elsereturn(((2*tn-1)*tx*p((tn-1),tx)-(tn-1)*p((tn-2),tx))/tn); }8.14#define N 10#define M 5float score[N][M];float a_stu[N],a_cor[M];main(){int i,j,r,c;float h;float s_diff();float highest();r=0;c=1;input_stu();avr_stu();avr_cor();printf("\n number class 1 2 3 4 5 avr");for(i=0;i<N;i++){printf("\nNO%2d",i+1);for(j=0;j<M;j++)printf("%8.2f",score[i][j]);printf("%8.2f",a_stu[i]);}printf("\nclassavr");for(j=0;j<M;j++)printf("%8.2f",a_cor[j]);h=highest(&r,&c);printf("\n\n%8.2f %d %d\n",h,r,c); printf("\n %8.2f\n",s_diff());}input_stu(){int i,j;float x;for(i=0;i<N;i++){for(j=0;j<M;j++){scanf("%f",&x);score[i][j]=x;}}}avr_stu(){int i,j;float s;for(i=0;i<N;i++){for(j=0,s=0;j<M;j++)s+=score[i][j];a_stu[i]=s/5.0;}}avr_cor(){int i,j;float s;for(j=0;j<M;j++){for(i=0,s=0;i<N;i++)s+=score[i][j];a_cor[j]=s/(float)N;}}float highest(r,c)int *r,*c;{float high;int i,j;high=score[0][0];for(i=0;i<N;i++)for(j=0;j<M;j++)if(score[i][j]>high){high=score[i][j];*r=i+1;*c=j+1;}return(high);}float s_diff(){int i,j;float sumx=0.0,sumxn=0.0;for(i=0;i<N;i++){sumx+=a_stu[i]*a_stu[i];sumxn+=a_stu[i];}return(sumx/N-(sumxn/N)*(sumxn/N)); }8.15#include<stdio.h>#define N 10void input_e(num,name)int num[];char name[N][8];{int i;for(i=0;i<N;i++){scanf("%d",&num[i]);gets(name[i]);}}void sort(num,name)int num[];char name[N][8];{int i,j,min,temp1;char temp2[8];for(i=0;i<N-1;i++){min=i;for(j=i;j<N;j++)if(num[min]>num[j])min=j;temp1=num[i];num[i]=num[min];num[min]=temp1;strcpy(temp2,name[i]);strcpy(name[i],name[min]);strcpy(name[min],temp2);}for(i=0;i<N;i++)printf("\n%5d%10s",num[i],name[i]);}void search(n,num,name)int n,num[];char name[N][8];{int top,bott,min,loca;loca=0;top=0;bott=N-1;if((n<num[0])||(n>num[N-1]))loca=-1;while((loca==0)&&(top<=bott)){min=(bott+top)/2;if(n==num[min]){loca=min;printf("number=%d,name=%s\n",n,name[loca]);}else if(n<num[min])bott=min-1;elsetop=min+1;}if(loca==0||loca==-1)printf("number=%d is not in table\n",n);}main(){int num[N],number,flag,c,n;char name[N][8];input_e(num,name);sort(num,name);for(flag=1;flag;){scanf("%d",&number);search(number,num,name);printf("continue?Y/N!");c=getchar();if(c=='N'||c=='n')flag=0;}}8.16#include<stdio.h>#define MAX 1000main(){int c,i,flag,flag1;char t[MAX];i=0;flag=0;flag1=1;while((c=getchar())!='\0'&&i<MAX&&flag1){if(c>='0'&&c<='9'||c>='A'&&c<='F'||c>='a'&&c<='f') {flag=1;t[i++]=c;}else if(flag){t[i]='\0';printf("\nnumber=%d\n",htoi(t));printf("continue?");c=getchar();if(c=='n'||c=='N')flag1=0;else{flag=0;i=0;}}}htoi(s)char s[];{int i,n;n=0;for(i=0;s[i]!='\0';i++){if(s[i]>='0'&&s[i]<='9')n=n*16+s[i]-'0';if(s[i]>='a'&&s[i]<='f')n=n*16+s[i]-'a'+10;if(s[i]>='A'&&s[i]<='F')n=n*16+s[i]-'A'+10;}return(n);}8.17#include<stdio.h>void convert(n)int n;{int i;if((i=n/10)!=0)convert(i);putchar(n%10+'0');}main(){int number;scanf("%d",&number);if(number<0){putchar('-');number=-number;}convert(number);}8.18main(){int year,month,day;int days;scanf("\n%d,%d,%d",&year,&month,&day); days=sum_day(month,day);if(leap(year)&&(month>=3))days+=1;printf("days=%d\n",days);。
C程序设计(第三版)谭浩强 清华大学出版社 课后习题答案
main()
{int a,b,c;
scanf("%d,%d,%d",&a,&b,&c);
if(a<b)
if(b<c)
printf("max=%d\n",c);
else
printf("max=%d\n",b);
else if(a<c)
printf("max=%d\n",c);
else
printf("max=%d\n",a);
C程序设计(第三版)谭浩强清华大学出版社课后习题答案
第一章
1.5
# include <stdio.h>
void main()
{
printf("* * * * * * * * *\n Very good!\n* * * * * * * * *\n");
}
1.6
(法一)
#include <stdio.h>
}
main()
{int a,b,c,temp,max;
scanf("%d,%d,%d",&a,&b,&c);
temp=(a>b)?a:b;
{float pi,h,r,l,s,sq,sv,sz;
pi=3.1415926;
printf("input r,h\n");
scanf("%f,%f",&r,&h);
l=2*pi*r;
s=r*r*pi;
sq=4*pi*r*r;
sv=4.0/3.0*pi*r*r*r;
谭浩强_C语言程序设计(第三版)课后习题答案
程序是在VC6.0下编译的,所以头文件和TC的不太一样!4-8#include <iostream>#define pi 3.1415void main(){float r,h,rl,ra,ss,sv,cv;printf("输入圆的半径和圆柱的高\n\n");scanf("%f%f",&r,&h);rl=2*pi*r;ra=pi*r*r;ss=4*pi*r*r;sv=4*pi*r*r*r/3;cv=ra*h;printf("圆周长为:%f\n\n圆面积为:%f\n\n圆球表面积为:%f\n\n圆球体积为:%f\n\n 圆柱体积为:%f\n\n",rl,ra,ss,sv,cv);}4-9#include <iostream>void main(){float F,c;printf("输入一个华氏温度\n\n");scanf("%f",&F);c=5*(F-32)/9;printf("摄氏温度为:%.2f\n\n",c);}第五章5-4#include <iostream>void main(){int a,b,c,max;printf("输入三个整数a,b,c\n\n"); scanf("%d%d%d",&a,&b,&c);if(a>b) max=a;else max=b;if(max<c) max=c;printf("最大的数为:%d\n\n",max); }5-5#include <iostream>void main(){float x,y;printf("输入x\n\n");scanf("%f",&x);if(x<1) y=x;else if(x<10) y=2*x-1;else y=3*x-11;printf("y=%f\n\n",y);}5-6#include <iostream>void main(){float m;int n;printf("输入成绩\n\n");scanf("%f",&m);n=m/10;switch(n){case 0:case 1:case 2:case 3:case 4:case 5:printf("E\n\n");break;case 6:printf("D\n\n");break;case 7:printf("C\n\n");break;case 8:printf("B\n\n");break;case 9:case 10:printf("A\n\n");break;}}5-7#include <iostream>#include <math.h>void main(){int a,b,c,i,j,n;printf("输入五位正整数\n\n");scanf("%d",&a);printf("\n\n");c=a;for(i=1;;i++){b=c%10;c=c/10;printf("%d",b);if(c==0){printf("\n\n");printf("正整数为%d位数\n\n",i);break; }}c=a;for(j=i-1;j>=0;j--){n=pow(10,j);b=c/n;c=c%n;printf("%d\n",b);}}5-8if语句:#include <iostream>void main(){int I;float bonus;printf("输入当月利润I\n\n");scanf("%d",&I);if(I<=100000) bonus=I/10;else if(I<=200000) bonus=10000+(I-100000)*7.5/100;else if(I<=400000) bonus=10000+7500+(I-200000)*5/100;else if(I<=600000) bonus=10000+7500+10000+(I-400000)*3/100;else if(I<=1000000) bonus=10000+7500+10000+6000+(I-600000)*1.5/100; else bonus=10000+7500+10000+6000+6000+(I-1000000)*1/100;printf("应发奖金数为:%f\n\n",bonus);}switch语句:#include <iostream>void main(){int I,n;float bonus;printf("输入当月利润I\n\n");scanf("%d",&I);if(I>1000000) n=10;else n=I/100000;switch(n){case 0:bonus=I/10;break;case 1:bonus=10000+(I-100000)*7.5/100;break;case 2:case 3:bonus=10000+7500+(I-200000)*5/100;break;case 4:case 5:bonus=10000+7500+10000+(I-400000)*3/100;break;case 6:case 7:case 8:case 9:bonus=10000+7500+10000+6000+(I-600000)*1.5/100;break;case 10:bonus=10000+7500+10000+6000+6000+(I-1000000)*1/100;break; }printf("应发奖金数为:%f\n\n",bonus);}5-9#include <iostream>void main(){int a,b,c,d,temp,min;printf("输入四个数a,b,c,d\n\n");scanf("%d%d%d%d",&a,&b,&c,&d);if(a>b){temp=a;a=b;b=temp;}if(a>c){temp=a;a=c;c=temp;}if(a>d){temp=a;a=d;d=temp;}printf("由小到大排列为:\n\n"); printf("%d ",a);if(b>c){temp=b;b=c;c=temp;}if(b>d){temp=b;b=d;d=temp;}printf("%d ",b);if(c>d){temp=c;c=d;d=temp;}printf("%d ",c);printf("%d",d);}5-10#include <iostream>#include <math.h>void main(){float x,y,a,b,c,d;printf("输入点的坐标\n\n");scanf("%f%f",&x,&y);a=sqrt((2-x)*(2-x)+(2-y)*(2-y));b=sqrt((2-x)*(2-x)+(-2-y)*(-2-y));c=sqrt((-2-x)*(-2-x)+(-2-y)*(-2-y));d=sqrt((-2-x)*(-2-x)+(2-y)*(2-y));if(a<=1||b<=1||c<=1||d<=1) printf("该点建筑高度为:10m"); else printf("该点建筑高度为:0m");}第六章:6-1#include <iostream>#include <math.h>void main(){int m,n,max,min,i,k;printf("输入两个正整数\n\n");scanf("%d%d",&m,&n);if(m>n) k=n;else k=m;for(i=1;i<=k;i++){if((m%i==0)&&(n%i==0)) max=i;}printf("最大公约数为:%d\n\n",max);if(m>n) k=m;else k=n;for(i=k;;i++){if((i%m==0)&&(i%n==0)){min=i;break;}}printf("最小公倍数为:%d\n\n",min);}6-2#include <iostream>void main(){char c;int l=0,b=0,n=0,e=0;printf("输入一行字符\n\n");while((c=getchar())!='\n'){if((c>='a'&&c<='z')||(c>='A'&&c<='Z')) l+=1;else if(c==' ') b+=1;else if(c>=48&&c<=57) n+=1;else e+=1;}printf("英文字母个数为:%d\n\n空格个数为:%d\n\n数字个数为:%d\n\n其他字符个数为:%d\n\n",l,b,n,e);}6-3#include <iostream>#include <math.h>void main(){int n,k,i;float a,s=0;printf("输入n和a\n\n");scanf("%d%f",&n,&a);for(i=0;i<n;i++){k=pow(10,i);s=s+(n-i)*a*k;}printf("和s=%f\n\n",s); }6-4#include <iostream>void main(){int n,s,S=0,i,j;printf("输入n\n\n");scanf("%d",&n);for(i=1;j<=n;i++){s=1;for(j=1;j<=i;j++){s=s*j;}S+=s;}printf("和S=%d\n\n",S); }6-5#include <iostream>void main(){int i,m,n,t;int s1=0,s2=0;float s3=0,s,l;printf("输入m,n,t的值\n\n");scanf("%d%d%d",&m,&n,&t);for(i=1;i<=m;i++){s1+=i;}for(i=1;i<=n;i++){s2+=i*i;}for(i=1;i<=t;i++){l=1.0/i;s3+=l;}s=s1+s2+s3;printf("和s=%f",s);}6-6#include <iostream>void main(){int m,n,k,i,j;printf("水仙花数为:\n");for(i=100;i<=999;i++){j=i;m=j%10;j=j/10;n=j%10;k=j/10;if(i==m*m*m+n*n*n+k*k*k) printf("%d ",i);}}6-7#include <iostream>#include <math.h>void main(){int i,j,k,s;for(i=1;i<=1000;i++){s=0;for(j=1;j<i;j++){if(i%j==0) s+=j;}if(s==i){printf("\n\n");printf("%d its factors are ",i); for(j=1;j<i;j++){if(i%j==0) printf("%d ",j);}}}}6-8#include <iostream>void main(){int n,i;float f1,f2,s,temp;printf("输入n\n\n");scanf("%d",&n);f1=2.0;f2=3.0;s=f1;for(i=1;i<=n-1;i++){s+=f2/f1;temp=f2;f2=f1+f2;f1=temp;}printf("前%d项和为%f",n,s);}6-9#include <iostream>#include <math.h>void main(){int i,n,l=100;float m,s=0;printf("输入n\n\n");scanf("%d",&n);m=l;for(i=1;i<=n;i++){s+=2*m;m=m*0.5;}printf("第%d次落地时经过%f米\n\n",n,s-l); printf("第%d次落地后反弹%f米\n\n",n,m); }6-10void main(){int m=1,n,i;printf("输入n\n\n");scanf("%d",&n);for(i=1;i<n;i++){m=(m+1)*2;}printf("一共摘了%d个桃子\n\n",m);}6-11#include <iostream>#include <math.h>void main(){float a,x1,x2;printf("输入a\n\n");scanf("%f",&a);x1=a/2;loop:x2=(x1+a/x1)/2;if(fabs(x1-x2)>1e-5){x1=x2;goto loop;}else printf("\n\na的平方根为%f\n\n",x2); }6-12#include <math.h>void main(){float f(float);float xpoint(float,float);float root(float,float);float x,x1,x2,f1,f2;do{printf("输入x1,x2\n\n");scanf("%f%f",&x1,&x2);f1=f(x1);f2=f(x2);}while(f1*f2>0);x=root(x1,x2);printf("方程在1.5附近的根为:%f\n\n",x); }float f(float x){float y;y=2*x*x*x-4*x*x+3*x-6;return(y);}float xpoint(float x1,float x2){float y;y=(x1*f(x2)-x2*f(x1))/(f(x2)-f(x1));return(y);}float root(float x1,float x2){float x,y,y1;y1=f(x1);do{x=xpoint(x1,x2);y=f(x);if(y*y1>0){y1=y;x1=x;}elsex2=x;}while(fabs(y)>1e-4);return(x);}6-13#include <iostream>#include <math.h>void main(){float f(float);float root(float,float);float x,x1,x2,f1,f2;do{printf("输入x1,x2\n\n");scanf("%f%f",&x1,&x2);f1=f(x1);f2=f(x2);}while(f1*f2>0);x=root(x1,x2);printf("方程的根为:%f\n\n",x); }float f(float x){float y;y=2*x*x*x-4*x*x+3*x-6;return(y);}float root(float x1,float x2){float x,y;do{y=f((x1+x2)/2);if(y<-1e-4){x1=(x1+x2)/2;}else if(y>1e-4){x2=(x1+x2)/2;}else x=(x1+x2)/2;}while(fabs(y)>1e-4);return(x);}6-14#include <iostream>void 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");}}6-15#include <iostream>void main(){char i,j,k;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("\nA--%c\nB--%c\nC--%c\n",i,j,k); }}}第七章7-1#include <iostream>#include <math.h>void main(){int i,j;int a[101];printf("素数为:\n\n");for(i=1;i<=100;i++){a[i]=i;}for(i=3;i<=100;i++){for(j=2;j<=sqrt(a[i]);j++){if(a[i]%j==0){a[i]=0;break;}}}for(i=2;i<=100;i++){if(a[i]!=0) printf("%d ",a[i]); }}7-2#include <iostream>void main(){int a[10],i,j,temp;printf("输入10个整数\n\n");for(i=0;i<10;i++){scanf("%d",&a[i]);}for(i=0;i<10;i++){for(j=i+1;j<10;j++){if(a[i]>a[j]){temp=a[i];a[i]=a[j];a[j]=temp;}}}printf("排序后的输出为:\n\n"); for(i=0;i<10;i++){printf("%d ",a[i]);}printf("\n\n");}7-3#include <iostream>void main(){int a[9],i,s=0;printf("输入一个3*3整型矩阵\n\n"); for(i=0;i<9;i++){scanf("%d",&a[i]);}for(i=0;i<9;i+=4){s+=a[i];}printf("对角线元素之和为:%d\n\n",s); }7-4#include <iostream>void main(){int a[11]={1,2,3,4,5,6,7,8,9,10};int i,j,n,temp1,temp2;printf("输入一个数n\n\n");scanf("%d",&n);if((a[0]-a[9])>0){if(n<=a[9]){a[10]=n;}else{for(i=0;i<10;i++)if(a[i]<n){temp1=a[i];a[i]=n;for(j=i+1;j<11;j++) {temp2=a[j];a[j]=temp1;temp1=temp2;}break;}}}}if((a[9]-a[0])>0){if(n>=a[9]){a[10]=n;}else{for(i=0;i<10;i++){if(a[i]>n){temp1=a[i];a[i]=n;for(j=i+1;j<11;j++) {temp2=a[j];a[j]=temp1;temp1=temp2;}break;}}}for(i=0;i<11;i++){printf("%d ",a[i]);}}7-5将一个数组中的值按逆序重新排列#include <iostream>#define N 5void main(){int a[N];int i,temp;printf("输入5个个数\n\n");for(i=0;i<N;i++){scanf("%d",&a[i]);}for(i=0;i<=N/2;i++){temp=a[i];a[i]=a[N-i-1];a[N-i-1]=temp;}for(i=0;i<N;i++){printf("%d ",a[i]);}printf("\n\n");}7-6输出杨辉三角形(要求输出10行)#include <iostream>#include <stdio.h>void main(){int a[10][10]={1};int i,j;for(i=1;i<10;i++){for(j=0;j<=i;j++){a[i][0]=a[i-1][0];a[i][j]=a[i-1][j-1]+a[i-1][j];}}for(i=0;i<10;i++){for(j=0;j<=i;j++){printf("%4d",a[i][j]);}printf("\n\n");}}7-7输出“魔方阵”。
C语言程序设计第三版谭浩强课后习题答案完整版
C语言程序设计第三版谭浩强课后习题答案完整版第一章1.5请参照本章例题,编写一个C程序,输出以下信息:**************************Very Good!**************************解:mian(){printf(“**************************”);printf(“\n”);printf(“Very Good!\n”);printf(“\n”);printf(“**************************”);} 1.6 编写一个程序,输入a、b、c三个值,输出其中最大值。
解:mian(){int a,b,c,max;printf(“请输入三个数a,b,c:\n”);scanf(“%d,%d,%d”,&a,&b,&c);max=a;if(max<b)max=b;if(max<c)max=c;printf(“最大数为:“%d”,max);}第三章3.6写出以下程序运行的结果。
main(){charc1=’a’,c2=’b’,c3=’c’,c4=’\101’,c5=’\116’;printf(“a%cb%c\tc%c\tabc\n”,c1,c2,c 3);printf(“\t\b%c %c”,c4,c5);}解:aaㄩbbㄩㄩㄩccㄩㄩㄩㄩㄩㄩabc AㄩN3.7 要将"China"译成密码,译码规律是:用原来字母后面的第4个字母代替原来的字母.例如,字母"A"后面第4个字母是"E"."E"代替"A"。
因此,"China"应译为"Glmre"。
请编一程序,用赋初值的方法使cl、c2、c3、c4、c5五个变量的值分别为,’C’、’h’、’i’、’n’、’a’,经过运算,使c1、c2、c3、c4、c5分别变为’G’、’l’、’m’、’r’、’e’,并输出。
C程序设计(谭浩强第三版)课后答案完整版
C语言程序设计(第三版)课后习题答案
C程序设计(第三版)答案-谭浩强程序是在VC6.0下编译的,所以头文件和TC的不太一样!4-8#include <iostream>#define pi 3.1415void main(){float r,h,rl,ra,ss,sv,cv;printf("输入圆的半径和圆柱的高\n\n");scanf("%f%f",&r,&h);rl=2*pi*r;ra=pi*r*r;ss=4*pi*r*r;sv=4*pi*r*r*r/3;cv=ra*h;printf("圆周长为:%f\n\n圆面积为:%f\n\n圆球表面积为:%f\n\n圆球体积为:%f\n\n 圆柱体积为:%f\n\n",rl,ra,ss,sv,cv);}4-9#include <iostream>void main(){float F,c;printf("输入一个华氏温度\n\n");scanf("%f",&F);c=5*(F-32)/9;printf("摄氏温度为:%.2f\n\n",c);}第五章5-4#include <iostream>void main(){int a,b,c,max;printf("输入三个整数a,b,c\n\n"); scanf("%d%d%d",&a,&b,&c);if(a>b) max=a;else max=b;if(max<c) max=c;printf("最大的数为:%d\n\n",max); }5-5#include <iostream>void main(){float x,y;printf("输入x\n\n");scanf("%f",&x);if(x<1) y=x;else if(x<10) y=2*x-1;else y=3*x-11;printf("y=%f\n\n",y);}5-6#include <iostream>void main(){float m;int n;printf("输入成绩\n\n");scanf("%f",&m);n=m/10;switch(n){case 0:case 1:case 2:case 3:case 4:case 5:printf("E\n\n");break;case 6:printf("D\n\n");break;case 7:printf("C\n\n");break;case 8:printf("B\n\n");break;case 9:case 10:printf("A\n\n");break;}}5-7#include <iostream>#include <math.h>void main(){int a,b,c,i,j,n;printf("输入五位正整数\n\n");scanf("%d",&a);printf("\n\n");c=a;for(i=1;;i++){b=c%10;c=c/10;printf("%d",b);if(c==0){printf("\n\n");printf("正整数为%d位数\n\n",i);break;}}c=a;for(j=i-1;j>=0;j--){n=pow(10,j);b=c/n;c=c%n;printf("%d\n",b);}}5-8if语句:#include <iostream>void main(){int I;float bonus;printf("输入当月利润I\n\n");scanf("%d",&I);if(I<=100000) bonus=I/10;else if(I<=200000) bonus=10000+(I-100000)*7.5/100;else if(I<=400000) bonus=10000+7500+(I-200000)*5/100;else if(I<=600000) bonus=10000+7500+10000+(I-400000)*3/100;else if(I<=1000000)bonus=10000+7500+10000+6000+(I-600000)*1.5/100;else bonus=10000+7500+10000+6000+6000+(I-1000000)*1/100; printf("应发奖金数为:%f\n\n",bonus);}switch语句:#include <iostream>void main(){int I,n;float bonus;printf("输入当月利润I\n\n");scanf("%d",&I);if(I>1000000) n=10;else n=I/100000;switch(n){case 0:bonus=I/10;break;case 1:bonus=10000+(I-100000)*7.5/100;break;case 2:case 3:bonus=10000+7500+(I-200000)*5/100;break;case 4:case 5:bonus=10000+7500+10000+(I-400000)*3/100;break;case 6:case 7:case 8:case 9:bonus=10000+7500+10000+6000+(I-600000)*1.5/100;break;case 10:bonus=10000+7500+10000+6000+6000+(I-1000000)*1/100;break; }printf("应发奖金数为:%f\n\n",bonus);}5-9#include <iostream>void main(){int a,b,c,d,temp,min;printf("输入四个数a,b,c,d\n\n");scanf("%d%d%d%d",&a,&b,&c,&d);if(a>b){temp=a;a=b;b=temp;}if(a>c){temp=a;a=c;c=temp;}if(a>d){temp=a;a=d;d=temp;}printf("由小到大排列为:\n\n"); printf("%d ",a);if(b>c){temp=b;b=c;c=temp;}if(b>d){temp=b;b=d;d=temp;}printf("%d ",b);if(c>d){temp=c;c=d;d=temp;}printf("%d ",c);printf("%d",d);}5-10#include <iostream>#include <math.h>void main(){float x,y,a,b,c,d;printf("输入点的坐标\n\n");scanf("%f%f",&x,&y);a=sqrt((2-x)*(2-x)+(2-y)*(2-y));b=sqrt((2-x)*(2-x)+(-2-y)*(-2-y));c=sqrt((-2-x)*(-2-x)+(-2-y)*(-2-y));d=sqrt((-2-x)*(-2-x)+(2-y)*(2-y));if(a<=1||b<=1||c<=1||d<=1) printf("该点建筑高度为:10m"); else printf("该点建筑高度为:0m");}第六章:6-1#include <iostream>#include <math.h>void main(){int m,n,max,min,i,k;printf("输入两个正整数\n\n");scanf("%d%d",&m,&n);if(m>n) k=n;else k=m;for(i=1;i<=k;i++){if((m%i==0)&&(n%i==0)) max=i;}printf("最大公约数为:%d\n\n",max);if(m>n) k=m;else k=n;for(i=k;;i++){if((i%m==0)&&(i%n==0)){min=i;break;}}printf("最小公倍数为:%d\n\n",min);}6-2#include <iostream>void main(){char c;int l=0,b=0,n=0,e=0;printf("输入一行字符\n\n");while((c=getchar())!='\n'){if((c>='a'&&c<='z')||(c>='A'&&c<='Z')) l+=1;else if(c==' ') b+=1;else if(c>=48&&c<=57) n+=1;else e+=1;}printf("英文字母个数为:%d\n\n空格个数为:%d\n\n数字个数为:%d\n\n其他字符个数为:%d\n\n",l,b,n,e);}6-3#include <iostream>#include <math.h>void main(){int n,k,i;float a,s=0;printf("输入n和a\n\n"); scanf("%d%f",&n,&a);for(i=0;i<n;i++){k=pow(10,i);s=s+(n-i)*a*k;}printf("和s=%f\n\n",s); }6-4#include <iostream>void main(){int n,s,S=0,i,j;printf("输入n\n\n"); scanf("%d",&n);for(i=1;j<=n;i++){s=1;for(j=1;j<=i;j++){s=s*j;}S+=s;}printf("和S=%d\n\n",S); }6-5#include <iostream>void main(){int i,m,n,t;int s1=0,s2=0;float s3=0,s,l;printf("输入m,n,t的值\n\n"); scanf("%d%d%d",&m,&n,&t);for(i=1;i<=m;i++){s1+=i;}for(i=1;i<=n;i++){s2+=i*i;}for(i=1;i<=t;i++){l=1.0/i;s3+=l;}s=s1+s2+s3;printf("和s=%f",s);}6-6#include <iostream>void main(){int m,n,k,i,j;printf("水仙花数为:\n");for(i=100;i<=999;i++){j=i;m=j%10;j=j/10;n=j%10;k=j/10;if(i==m*m*m+n*n*n+k*k*k) printf("%d ",i); }}6-7#include <iostream>#include <math.h>void main(){int i,j,k,s;for(i=1;i<=1000;i++){s=0;for(j=1;j<i;j++){if(i%j==0) s+=j;}if(s==i){printf("\n\n");printf("%d its factors are ",i);for(j=1;j<i;j++){if(i%j==0) printf("%d ",j);}}}}6-8#include <iostream>void main(){int n,i;float f1,f2,s,temp;printf("输入n\n\n");scanf("%d",&n);f1=2.0;f2=3.0;s=f1;for(i=1;i<=n-1;i++){s+=f2/f1;temp=f2;f2=f1+f2;f1=temp;}printf("前%d项和为%f",n,s);}6-9#include <iostream>#include <math.h>void main(){int i,n,l=100;float m,s=0;printf("输入n\n\n");scanf("%d",&n);m=l;for(i=1;i<=n;i++){s+=2*m;m=m*0.5;}printf("第%d次落地时经过%f米\n\n",n,s-l); printf("第%d次落地后反弹%f米\n\n",n,m); }6-10void main(){int m=1,n,i;printf("输入n\n\n");scanf("%d",&n);for(i=1;i<n;i++){m=(m+1)*2;}printf("一共摘了%d个桃子\n\n",m);}6-11#include <iostream>#include <math.h>void main(){float a,x1,x2;printf("输入a\n\n");scanf("%f",&a);x1=a/2;loop:x2=(x1+a/x1)/2;if(fabs(x1-x2)>1e-5){x1=x2;goto loop;}else printf("\n\na的平方根为%f\n\n",x2); }6-12#include <math.h>void main(){float f(float);float xpoint(float,float);float root(float,float);float x,x1,x2,f1,f2;do{printf("输入x1,x2\n\n");scanf("%f%f",&x1,&x2);f1=f(x1);f2=f(x2);}while(f1*f2>0);x=root(x1,x2);printf("方程在1.5附近的根为:%f\n\n",x); }float f(float x){float y;y=2*x*x*x-4*x*x+3*x-6;return(y);}float xpoint(float x1,float x2){float y;y=(x1*f(x2)-x2*f(x1))/(f(x2)-f(x1)); return(y);}float root(float x1,float x2){float x,y,y1;y1=f(x1);do{x=xpoint(x1,x2);y=f(x);if(y*y1>0){y1=y;x1=x;}elsex2=x;}while(fabs(y)>1e-4);return(x);}6-13#include <iostream>#include <math.h>void main(){float f(float);float root(float,float);float x,x1,x2,f1,f2;do{printf("输入x1,x2\n\n");scanf("%f%f",&x1,&x2);f1=f(x1);f2=f(x2);}while(f1*f2>0);x=root(x1,x2);printf("方程的根为:%f\n\n",x); }float f(float x){float y;y=2*x*x*x-4*x*x+3*x-6;return(y);}float root(float x1,float x2){float x,y;do{y=f((x1+x2)/2);if(y<-1e-4){x1=(x1+x2)/2;}else if(y>1e-4){x2=(x1+x2)/2;}else x=(x1+x2)/2;}while(fabs(y)>1e-4);return(x);}6-14#include <iostream>void 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");}}6-15#include <iostream>void main(){char i,j,k;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("\nA--%c\nB--%c\nC--%c\n",i,j,k);}}}}第七章7-1#include <iostream>#include <math.h>void main(){int i,j;int a[101];printf("素数为:\n\n");for(i=1;i<=100;i++){a[i]=i;}for(i=3;i<=100;i++){for(j=2;j<=sqrt(a[i]);j++){if(a[i]%j==0){a[i]=0;break;}}}for(i=2;i<=100;i++){if(a[i]!=0) printf("%d ",a[i]); }}7-2#include <iostream>void main(){int a[10],i,j,temp;printf("输入10个整数\n\n");for(i=0;i<10;i++){scanf("%d",&a[i]);}for(i=0;i<10;i++){for(j=i+1;j<10;j++){if(a[i]>a[j]){temp=a[i];a[i]=a[j];a[j]=temp;}}}printf("排序后的输出为:\n\n"); for(i=0;i<10;i++){printf("%d ",a[i]);}printf("\n\n");}7-3#include <iostream>void main(){int a[9],i,s=0;printf("输入一个3*3整型矩阵\n\n"); for(i=0;i<9;i++){scanf("%d",&a[i]);}for(i=0;i<9;i+=4){s+=a[i];}printf("对角线元素之和为:%d\n\n",s); }7-4#include <iostream>void main(){int a[11]={1,2,3,4,5,6,7,8,9,10};int i,j,n,temp1,temp2;printf("输入一个数n\n\n");scanf("%d",&n);if((a[0]-a[9])>0){if(n<=a[9]){a[10]=n;}else{for(i=0;i<10;i++)if(a[i]<n){temp1=a[i];a[i]=n;for(j=i+1;j<11;j++){temp2=a[j];a[j]=temp1;temp1=temp2;}break;}}}}if((a[9]-a[0])>0){if(n>=a[9]){a[10]=n;}else{for(i=0;i<10;i++){if(a[i]>n){temp1=a[i];a[i]=n;for(j=i+1;j<11;j++){temp2=a[j];a[j]=temp1;temp1=temp2;}break;}}}for(i=0;i<11;i++){printf("%d ",a[i]);}}7-5#include <iostream>#define N 5void main(){int a[N];int i,temp;printf("输入5个个数\n\n"); for(i=0;i<N;i++){scanf("%d",&a[i]);}for(i=0;i<=N/2;i++){temp=a[i];a[i]=a[N-i-1];a[N-i-1]=temp;}for(i=0;i<N;i++){printf("%d ",a[i]);}printf("\n\n");}7-6#include <iostream>#include <stdio.h>void main(){int a[10][10]={1};int i,j;for(i=1;i<10;i++){for(j=0;j<=i;j++){a[i][0]=a[i-1][0];a[i][j]=a[i-1][j-1]+a[i-1][j];}}for(i=0;i<10;i++){for(j=0;j<=i;j++){printf("%4d",a[i][j]);}printf("\n\n");}}7-7#include <iostream>#define N 7void main(){int a[N][N]={0};int i,j,k;i=0;j=(N-1)/2;a[i][j]=1;for(k=2;k<=N*N;k++) {i=i-1;j=j+1;if((i<0)&&(j>N-1)){i=i+2;j=j-1;}else{if(i<0) i=N-1;if(j>N-1) j=0;}if(a[i][j]==0){a[i][j]=k;}else{i=i+2;j=j-1;a[i][j]=k;}}for(i=0;i<N;i++){for(j=0;j<N;j++){printf("%4d",a[i][j]);}printf("\n\n");}}7-8#include <iostream>void main(){int a[4][4],i,j,max,min,k1,k2,flag; printf("输入一个4*4的数组\n\n"); for(i=0;i<4;i++){for(j=0;j<4;j++){scanf("%d",&a[i][j]);}}printf("鞍点为:\n\n");for(i=0;i<4;i++){max=a[i][0];for(j=0;j<4;j++){if(max<=a[i][j]){max=a[i][j];k1=j;}}min=a[0][k1];for(j=0;j<4;j++)if(min>=a[j][k1]){min=a[j][k1];k2=j;}}if((max!=min)) flag=0;else{flag=1;printf("%d\n",max);break;}}if(flag==0) printf("不存在\n\n");}7-9#include <iostream>void main(){float a[15]={100,98,65,55,34,23,23,12,11,9,7,6,4,3,2},n; int i,low,high,mid;printf("原数组为:\n\n");for(i=0;i<15;i++){printf("%6.2f",a[i]);}printf("\n\n输入一个数n\n\n");scanf("%f",&n);low=0;high=14;while(low<=high){mid=(low+high)/2;if(a[mid]==n){printf("该数是数组中第%d个元素\n\n",mid+1);i++;break;}else if(a[mid]<n) high=mid-1;else low=mid+1;}if(i==0) printf("无此数!!\n\n");}7-10#include <iostream>void main(){char a[3][80];int i,j,upon=0,low=0,num=0,spa=0,oth=0;for(i=0;i<3;i++){printf("输入第%d行文字\n\n",i);gets(a[i]);for(j=0;j<80&&a[i][j]!='\0';j++){if(a[i][j]>='A'&&a[i][j]<='Z') upon+=1;else if(a[i][j]>='a'&&a[i][j]<='z') low+=1;else if(a[i][j]>=48&&a[i][j]<=57) num+=1;else if(a[i][j]==' ') spa+=1;else oth+=1;}printf("英文大写字母:%d个\n\n",upon);printf("英文小写字母:%d个\n\n",low);printf("数字: %d个\n\n",num); printf("空格: %d个\n\n",spa); printf("其他字符: %d个\n\n",oth);}7-11#include <iostream>void main(){int i,j;for(i=0;i<5;i++){for(j=0;j<=i;j++){printf(" ");}for(j=i;j<i+5;j++){printf("* ");}printf("\n");}}7-12#include <iostream>void main(){char a[80],b[80];int i,j;printf("输入密码\n\n");gets(a);for(i=0;i<80&&a[i]!='\0';i++){if((a[i]>='A')&&(a[i]<='Z')){b[i]=155-a[i];}else if((a[i]>='a')&&(a[i]<='z')){b[i]=219-a[i];}else b[i]=a[i];}b[i]='\0';printf("密文为:\n\n");puts(a);printf("原文为:\n\n");puts(b);}7-13#include <iostream>void main(){char a[80],b[80];int i,j,k;printf("输入第1个字符串\n\n");gets(a);printf("输入第2个字符串\n\n");gets(b);for(i=0;i<80;i++){if(a[i]=='\0'){k=i;for(j=0;j<80&&b[j]!='\0';j++){a[k]=b[j];k++;}a[k]='\0';break;}}printf("连接后字符串为:\n\n");puts(a);}7-14#include <iostream>void main(){char a[80],b[80];int i,j,k;printf("输入第1个字符串\n\n");gets(a);printf("输入第2个字符串\n\n");gets(b);for(i=0;(i<80)&&(a[i]!='\0')&&(b[i]!='\0');i++) {k=a[i]-b[i];if(k>0){printf("\n\n字符串a大于字符串b:%4d\n\n",k);break;}else if(k<0){printf("\n\n字符串a小于字符串b:%4d\n\n",k);break;}else k=0;}if(k==0) printf("\n\n字符串a等于字符串b:%4d\n\n",k); }7-15#include <iostream>void main(){char s1[80],s2[80];int i,j;printf("输入数组s1\n\n");gets(s1);printf("输入数组s2\n\n");gets(s2);for(i=0;i<80&&s2[i]!='\0';i++){s1[i]=s2[i];}s1[i]='\0';printf("\n\n复制后的数组为:\n\n");puts(s1);}第八章8-1#include <iostream>void main(){int max_div(int,int);int min_mul(int,int);int m,n;printf("输入两个整数\n\n");scanf("%d%d",&m,&n);if(max_div(m,n)==0) printf("\n\n无最大公约数为\n\n"); else printf("\n\n最大公倍数为:%d\n\n",max_div(m,n)); printf("\n\n最大公倍数为:%d\n\n",min_mul(m,n));}int max_div(int k1,int k2){int min(int,int);int i,k,flag;k=min(k1,k2);for(i=k;i>1;i--){if((k1%i==0)&&(k2%i==0)){return(i);flag=1;break;}else flag=0;}if(flag==0) return(0);}int min_mul(int k1,int k2){int max(int,int);int i,k;k=max(k1,k2);for(i=k;;i++){if((i%k1==0)&&(i%k2==0)){return(i);}}}int min(int k1,int k2){int min;min=k1;if(k1>k2) min=k2;return(min);}int max(int k1,int k2){int max;max=k1;if(k1<k2) max=k2;return(max);}8-2#include <iostream>#include <math.h>void main(){void root1(float,float,float);void root2(float,float,float);void root3(float,float,float);float a,b,c,k;printf("输入a,b,c的值\n\n");scanf("%f%f%f",&a,&b,&c);k=b*b-4*a*c;if(k>0) root1(a,b,c);else if(k==0) root2(a,b,c);else root3(a,b,c);}void root1(float a,float b,float c){float p,q;p=-b/(2*a);q=sqrt(b*b-4*a*c)/(2*a);printf("\n\n方程的两个根为:x1=%6.2f,x2=%6.2f\n\n",p+q,p-q); }void root2(float a,float b,float c){p=-b/(2*a);printf("\n\n方程的两个根为:x1=x2=%6.2f\n\n",p);}void root3(float a,float b,float c){float p,q;p=-b/(2*a);q=sqrt(-b*b+4*a*c)/(2*a);printf("\n\n方程的两个根为:x1=%6.2f+%6.2fi,x2=%6.2f-%6.2fi\n\n",p,q,p,q); }8-3#include <iostream>void main(){int prime(int);int x;printf("输入一个正整数\n\n");scanf("%d",&x);if(prime(x)==0) printf("\n\n此数为素数\n\n");else printf("\n\n此数不是素数\n\n");}int prime(int x){int i,flag;for(i=2;i<x;i++){if(x%i==0){flag=1;break;}else flag=0;}return(flag);}8-4#include <iostream>void main(){void exchange(int a[3][3]);int a[3][3],i,j;printf("输入一个3*3的矩阵\n\n"); for(i=0;i<3;i++){for(j=0;j<3;j++){scanf("%d",&a[i][j]);}}exchange(a);printf("\n\n转置后的矩阵为:\n\n"); for(i=0;i<3;i++){for(j=0;j<3;j++){printf("%d ",a[i][j]);}printf("\n");}}8-5#include <iostream>void main(){void exchange(char str[]);char str[80];printf("输入一个字符串\n\n");gets(str);exchange(str);printf("\n\n反序字符串为:\n\n"); puts(str);}void exchange(char str[]){int i,j,k;char temp;for(i=0;;i++){if(str[i]=='\0') break;}k=i/2;for(j=0;j<=k;j++){temp=str[j];str[j]=str[i-1-j];str[i-1-j]=temp;}str[i]='\0';}8-6#include <iostream>void main(){void strcat(char str1[],char str2[]);char str1[80],str2[80];printf("输入str1\n\n");gets(str1);printf("\n\n输入str2\n\n");gets(str2);strcat(str1,str2);printf("\n\n连接后的字符串为:\n\n"); puts(str1);}void strcat(char str1[],char str2[]){int i,j;for(i=0;;i++){if(str1[i]=='\0'){for(j=0;str2[j]!='\0';j++){str1[i]=str2[j];i+=1;}break;}}str1[i]='\0';}8-7#include <iostream>void main(){void strcpy(char str1[],char str2[]);char str1[80],str2[80];printf("输入str2\n\n");gets(str2);strcpy(str1,str2);printf("\n\n复制后的字符串为:\n\n");puts(str1);}void strcpy(char str1[],char str2[]){int i,j=0;for(i=0;str2[i]!='\0';i++){if((str2[i]=='a')||(str2[i]=='e')||(str2[i]=='i')||(str2[i]=='o')||(str2[i]=='u')) {str1[j]=str2[i];j++;}}str1[j]='\0';}8-8#include <iostream>void main(){void stradd(char str[]);char str[80];printf("输入一串数字\n\n");gets(str);stradd(str);printf("\n\n加空格后的字符串\n\n");puts(str);}void stradd(char str[]){char a[80];int i=0,j;for(j=0;str[j]!='\0';j++){a[i]=str[j];a[i+1]=' ';i+=2;}a[i]='\0';for(i=0;a[i]!='\0';i++){str[i]=a[i];}str[i]='\0';}8-9#include <iostream>void main(){void num(char str[],int a[]);char str[80];int a[4]={0};printf("输入一个字符串\n\n");gets(str);num(str,a);printf("字母个数为:%d\n",a[0]);printf("数字个数为:%d\n",a[1]);printf("空格个数为:%d\n",a[2]);printf("其它字符个数为:%d\n",a[3]);}void num(char str[],int a[]){int i,word=0,num=0,span=0,oth=0;for(i=0;str[i]!='\0';i++){if((str[i]>='A'&&str[i]<='Z')||(str[i]>='a'&&str[i]<='z')) word+=1; else if(str[i]>=48&&str[i]<=57) num+=1;else if(str[i]==' ') span+=1;else oth+=1;}a[0]=word;a[1]=num;a[2]=span;a[3]=oth;}8-10#include <iostream>void main(){int word(char c);int longest(char str[]);int i;printf("输入一行字符串\n\n");gets(str);printf("\n\n最长的单词为:\n\n");for(i=longest(str);word(str[i]);i++)printf("%c",str[i]);printf("\n");}int word(char c){if((c>='a'&&c<='z')||(c>='A'&&c<='Z')) return 1; else return 0;}int longest(char str[]){int i,flag=1,len=0,point,max_len=0,max_point; for(i=0;i<=strlen(str);i++){if(word(str[i])){if(flag==1){point=i;flag=0;}else len++;}else{flag=1;if(len>max_len){max_len=len;max_point=point;}len=0;}}return (max_point);}8-11#include <iostream>{char str[80];void sort(char str[]);printf("输入一个字符串\n\n");gets(str);sort(str);printf("\n\n字符由小到大排序为:\n\n");puts(str);}void sort(char str[]){int i,j;char temp;for(i=0;i<strlen(str);i++){for(j=0;j<strlen(str)-i-1;j++){if(str[j]>str[j+1]){temp=str[j];str[j]=str[j+1];str[j+1]=temp;}}}}8-12#include <iostream>#include <math.h>void main(){int a,b,c,d;float root(int a,int b,int c,int d);printf("输入a,b,c,d\n\n");scanf("%d%d%d%d",&a,&b,&c,&d);printf("\n\n在1附近的根为:%f\n\n",root(a,b,c,d)); }float root(int a,int b,int c,int d){float x=1,x1,f,f1;do{x1=x;f=((a*x1+b)*x1+c)*x1+d;f1=(a*x1+b)*x1+c;x=x1-f/f1;}while(fabs(x-x1)>=1e-5);return(x);}8-13#include <iostream>void main(){float p(int,float);int n;float x;printf("输入n和x\n\n");scanf("%d%f",&n,&x);printf("\n\n%d阶勒德让多项式的值为:%f\n\n",n,p(n,x)); }float p(int n,float x){if(n==0) return 1;else if(n==1) return x;else return ((2*n-1)*x-p(n-1,x)-(n-1)*p(n-2,x))/n;}8-14#include <iostream>#define n1 2#define n2 5float a_stu[n1],a_cou[n2];void main(){void input(float a[][n2]);void stu_aver(float a[][n2]);void cou_aver(float a[][n2]);int hightest(int *s,int *c,float a[][n2]);float d(float a[][n2]);int i,j,s=0,c=0;float score[n1][n2],h;input(score);stu_aver(score);cou_aver(score);printf("\n\n 序号课程1 2 3 4 5 平均成绩\n\n");for(i=0;i<n1;i++){printf(" NO.%d ",i+1);for(j=0;j<n2;j++){printf("%8.2f",score[i][j]);}printf("%8.2f\n\n",a_stu[i]);}printf("课程平均");for(i=0;i<n2;i++){printf("%8.2f",a_cou[i]);}h=hightest(&s,&c,score);printf("\n\n最高成绩为:%f 是第%d号学生,第%d门课的成绩\n\n",h,s,c); printf("平均方差为:%f\n\n",d(score));}void input(float a[][n2]){int i,j;for(i=0;i<n1;i++){printf("\n\n输入%d个学生的5个成绩\n\n",i+1);for(j=0;j<n2;j++){scanf("%f",&a[i][j]);}}}void stu_aver(float a[][n2]){int i,j;float s;for(i=0;i<n1;i++){s=0;for(j=0;j<n2;j++){s+=a[i][j];}a_stu[i]=s/n2;}void cou_aver(float a[][n2]){int i,j;float s;for(i=0;i<n2;i++){s=0;for(j=0;j<n1;j++){s+=a[j][i];}a_cou[i]=s/n1;}}int hightest(int *s,int *c,float a[][n2]) {float hightest=0;int i,j;for(i=0;i<n1;i++){for(j=0;j<n2;j++){if(a[i][j]>hightest){hightest=a[i][j];*s=i+1;*c=j+1;}}}return(hightest);}float d(float a[][n2]){int i;float x=0,y=0;for(i=0;i<n1;i++){x+=a_stu[i]*a_stu[i];y+=a_stu[i];}return(x/n1-(y/n1)*(y/n1));8-15#include <iostream>#define N 3void main(){void input(int num[],char name[N][8]);void sort(int num[],char name[N][8]);void search(int num[],char name[N][8],int n); int flag,n,num[N];char c,name[N][8];input(num,name);sort(num,name);for(flag=1;flag;){printf("输入要查找的职工号\n\n");scanf("%d",&n);search(num,name,n);printf("是否继续查找?Y/N ?\n\n");c=getchar();if(c=='N'||c=='n') flag=0;}}void input(int num[],char name[N][8]){int i;for(i=0;i<N;i++){printf("输入职工号\n\n");scanf("%d",&num[i]);printf("输入职工姓名\n\n");getchar();gets(name[i]);}}void sort(int num[],char name[N][8]){int i,j,min,temp1;char temp2[8];for(i=0;i<N;i++){min=i;。
谭浩强C程序的设计第三版习题答案(全)
谭浩强《C程序设计》第三版习题答案(全)第一章1.5#include <stdio.h>void main(){printf("* * * * * * * * *\n Very good!\n* * * * * * * * *\n");}1.6(法一)#include <stdio.h>main(){int a,b,c,max;scanf("%d,%d,%d",&a,&b,&c);max=(a>b)?a:b;max=(max>c)?max:c;printf("max=%d\n",max);}(法二)main(){int a,b,c,max;scanf("%d,%d,%d",&a,&b,&c);if(a>b&&a>c)max=a;else if (b>c)max=b;elsemax=c;printf("max=%d\n",max);}(法三)# include <stdio.h>void main(){int max(int x,int y,int z);int a,b,c,d;scanf("%d,%d,%d",&a,&b,&c);d=max(a,b,c);printf("max=%d\n",d);}int max(int x,int y,int z) {int A,B;if(x>y) A=x;else A=y;if(z>A) B=z;else B=A;return(B);}第三章3.3(1)(10)10=(12)8=(a)16(2)(32)10=(40)8=(20)16(3)(75)10=(113)8=(4b)16(4)(-617)10=(176627)8=(fd97)16(5)(-111)10=(177621)8=(ff91)16(6)(2483)10=(4663)8=(963)16(7)(-28654)10=(110022)8=(9012)16(8)(21003)10=(51013)8=(520b)163.6aabb (8)cc (8)abc(7)AN3.7main(){char c1='C',c2='h',c3='i',c4='n',c5='a'; c1+=4, c2+=4, c3+=4, c4+=4, c5+=4;printf("%c%c%c%c%c\n",c1,c2,c3,c4,c5);}3.8main(){int c1,c2;c1=97;c2=98;printf("%c %c",c1,c2);}3.9(2)=3.53.109,11,9,103.12(1)24 (2)10 (3)60 (4)0 (5)0 (6)0第四章4.4main(){int a,b,c;long int u,n;float x,y,z;char c1,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=%2d b=%2d c=%2d\n",a,b,c);printf("x=%8.6f,y=%8.6f,z=%9.6f\n",x,y,z);printf("x+y=%5.2f y+z=%5.2f z+x=%5.2f\n",x+y,y+z,z+x);printf("u=%6ld n=%9ld\n",u,n);printf("c1='%c'or %d(ASCII)\n",c1,c1);printf("c2='%c'or %d(ASCII)\n",c2,c2);}4.5575 767.856400,-789.12396267.856400,-789.12396267.86 -789.12,67.856400,-789.123962,67.856400,-789.123962 6.785640e+001,-7.89e+002A,65,101,411234567,4553207,d68765535,177777,ffff,-1COMPUTER, COM4.6x=8.5 y=71.82/c1=A c2=a/4.710 20Aa1.5 -3.75 +1.4,67.8/(空3)10(空3)20Aa1.5(空1)-3.75(空1)(随意输入一个数),67.8回车4.8main(){float pi,h,r,l,s,sq,sv,sz;pi=3.1415926;printf("input r,h\n");scanf("%f,%f",&r,&h);l=2*pi*r;s=r*r*pi;sq=4*pi*r*r;sv=4.0/3.0*pi*r*r*r;sz=pi*r*r*h;printf("l=%6.2f\n",l);printf("s=%6.2f\n",s);printf("sq=%6.2f\n",sq);printf("vq=%6.2f\n",sv);printf("vz=%6.2f\n",sz);}4.9main(){float c,f;scanf("%f",&f);c=(5.0/9.0)*(f-32);printf("c=%5.2f\n",c);}4.10#include"stdio.h"main(){char c1,c2;scanf("%c,%c",&c1,&c2);putchar(c1);putchar(c2);printf("\n");printf("%c%c\n",c1,c2);}第五章5.3(1)0 (2)1 (3)1 (4)0 (5)15.4main(){int a,b,c;scanf("%d,%d,%d",&a,&b,&c); if(a<b)if(b<c)printf("max=%d\n",c); elseprintf("max=%d\n",b); else if(a<c)printf("max=%d\n",c); elseprintf("max=%d\n",a); }main(){int a,b,c,temp,max;scanf("%d,%d,%d",&a,&b,&c); temp=(a>b)?a:b;max=(c>temp)?c:temp;printf("max=%d",max);}5.5main(){int x,y;scanf("%d",&x);if(x<1)y=x;else if(x<10)y=2*x-1;else y=3*x-11;printf("y=%d",y);}5.6main(){int score,temp,logic;char grade;logic=1;while(logic){scanf("%d",&score);if(score>=0&&score<=100)logic=0;}if(score==100)temp=9;elsetemp=(score-score%10)/10;switch(temp){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"score=%d,grade=%c",score,grade);}5.7main(){long int num;int indiv,ten,hundred,thousand,ten_thousand,place;scanf("%ld",&num);if(num>9999) place=5;else if(num>999) place=4;else if(num>99) place=3;else if(num>9) place=2;else place=1;printf("place=%d\n",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;switch(place){case5:printf("%d,%d,%d,%d,%d\n",ten_thousand,thousand,hundred,ten,indiv);printf("%d,%d,%d,%d,%d\n",indiv,ten,hundred,thousand,ten_thousand); break;case 4:printf("%d,%d,%d,%d\n",thousand,hundred,ten,indiv); printf("%d,%d,%d,%d\n",indiv,ten,hundred,thousand); break;case 3:printf("%d,%d,%d\n",hundred,ten,indiv);printf("%d,%d,%d\n",indiv,ten,hundred);break;case 2:printf("%d,%d\n",ten,indiv);printf("%d,%d\n",indiv,ten);break;case 1:printf("%d\n",indiv);printf("%d\n",indiv);}}5.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",&i);if(i<=1e5)bonus=i*0.1;else if(i<=2e5)bonus=bon1+(i-100000)*0.075;else if(i<=4e5)bonus=bon2+(i-200000)*0.05;else if(i<=6e5)bonus=bon4+(i-400000)*0.03;else if(i<=1e6)bonus=bon6+(i-600000)*0.015;else bonus=bon10+(i-1000000)*0.01;printf("bonus=%10.2f",bonus);}main(){long i;float 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;scanf("%ld",&i);branch=i/100000;if(branch>10)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;}printf("bonus=%10.2f",bonus);}4.9main(){int t,a,b,c,d;scanf("%d,%d,%d,%d",&a,&b,&c,&d);if(a>b){t=a;a=b;b=t;}if(a>c){t=a;a=c;c=t;}if(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("%d %d %d %d\n",a,b,c,d);}5.10main(){int h=10;float x,y,x0=2,y0=2,d1,d2,d3,d4;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("h=%d",h);}第六章6.1main(){int a,b,num1,num2,temp;scanf("%d,%d",&num1,&num2);if(num1<num2){temp=num1;num1=num2;num2=temp;}a=num1;b=num2;while(b!=0){temp=a%b;a=b;b=temp;}printf("%d\n",a);printf("%d\n",num1*num2/a);}6.2#include"stdio.h"main(){char c;int letter=0,space=0,digit=0,other=0;while((c=getchar())!='\n'){if(c>='a'&&c<='z'||c>='A'&&c<='Z') letter++;else if(c==' ')space++;else if(c>='0'&&c<='9')digit++;else other++;}printf("letter=%d\nspace=%d\ndigit=%d\nother=%d\n",letter,space,digit ,other);}6.3main(){int a,n,count=1,sn=0,tn=0;scanf("%d,%d",&a,&n);while(count<=n){tn+=a;sn+=tn;a*=10;++count;}printf("a+aa+aaa+...=%d\n",sn);}6.4main(){float n,s=0,t=1;for(n=1;n<=20;n++){t*=n;s+=t;}printf("s=%e\n",s);}6.5main(){int N1=100,N2=50,N3=10;float k;float s1=0,s2=0,s3=0;for(k=1;k<=N1;k++)s1+=k;for(k=1;k<=N2;k++)s2+=k*k;for(k=1;k<=N3;k++)s3+=1/k;printf("s=%8.2f\n",s1+s2+s3);}6.6main(){int i,j,k,n;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("n=%d\n",n);}}6.7#define M 1000main(){int k0,k1,k2,k3,k4,k5,k6,k7,k8,k9;int i,j,n,s;for(j=2;j<=M;j++){n=0;s=j;for(i=1;i<j;i++){if((j%i)==0){n++;s=s-i;switch(n){case 1:k0=i;break;case 2:k1=i;break;case 3:k2=i;break;case 4:k3=i;break;case 5:k4=i;break;case 6:k5=i;break;case 7:k6=i;break;case 8:k7=i;break;case 9:k8=i;break;case 10:k9=i;break;}}}if(s==0){printf("j=%d\n",j);if(n>1)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\n",k9);}}}main(){static int k[10];int i,j,n,s;for(j=2;j<=1000;j++){n=-1;s=j;for(i=1;i<j;i++){if((j%i)==0){n++;s=s-i;k[n]=i;}}if(s==0){printf("j=%d\n",j); for(i=0;i<n;i++)printf("%d,",k[i]); printf("%d\n",k[n]); }}}6.8main(){int n,t,number=20;float a=2;b=1;s=0;for(n=1;n<=number;n++){s=s+a/b;t=a,a=a+b,b=t;}printf("s=%9.6f\n",s);}6.9main(){float sn=100.0,hn=sn/2;int n;for(n=2;n<=10;n++){sn=sn+2*hn;hn=hn/2;}printf("sn=%f\n",sn);printf("hn=%f\n",hn);}6.10main(){int day,x1,x2;day=9;x2=1;while(day>0){x1=(x2+1)*2;x2=x1;day--;}printf("x1=%d\n",x1);}6.11#include"math.h"main(){float a,xn0,xn1;scanf("%f",&a);xn0=a/2;xn1=(xn0+a/xn0)/2;do{xn0=xn1;xn1=(xn0+a/xn0)/2;}while(fabs(xn0-xn1)>=1e-5);printf("a=%5.2f\n,xn1=%8.2f\n",a,xn1); }6.12#include"math.h"main(){float x,x0,f,f1;x=1.5;do{x0=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.2f\n",x);}6.13#include"math.h"main(){float x0,x1,x2,fx0,fx1,fx2;do{scanf("%f,%f",&x1,&x2);fx1=x1*((2*x1-4)*x1+3)-6;fx2=x2*((2*x2-4)*x2+3)-6;}while(fx1*fx2>0);do{x0=(x1+x2)/2;fx0=x0*((2*x0-4)*x0+3)-6;if((fx0*fx1)<0){x2=x0;fx2=fx0;}else{x1=x0;fx1=fx0;}}while(fabs(fx0)>=1e-5);printf("x0=%6.2f\n",x0);}6.14main(){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");}}6.15main(){char i,j,k;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("\na--%c\tb--%c\tc--%c\n",i,j,k); }}}}第七章7.1#include <math.h>#define N 101main(){ int i,j,line,a[N];for (i=2;i<N;i++) a[i]=i;for (i=2;i<sqrt(N);i++)for (j=i+1;j<N;j++){if(a[i]!=0 && a[j]!=0) if (a[j]%a[i]==0)a[j]=0; } printf("\n");for (i=2,line=0;i<N;i++){ if(a[i]!=0){ printf("%5d",a[i]); line++; }if(line==10){ printf("\n");line=0; }}}7.2#define N 10main(){int i,j,min,temp,a[N];for(i=0;i<N;i++)scanf("%d",&a[i]);for(i=0;i<N-1;i++){min=i;for(j=i+1;j<N;j++)if(a[min]>a[j])min=j; temp=a[i];a[i]=a[min];a[min]=temp;}for(i=0;i<N;i++)printf("%5d",a[i]);}7.3main(){float a[3][3],sum;int i,j;for(i=0;i<3;i++)for(j=0;j<3;j++){scanf("%f",&sum);a[i][j]=sum;}for(i=0;i<3;i++)sum=sum+a[i][i];printf("sum=%f",sum);}7.4main(){int a[11]={1,4,6,9,13,16,19,28,40,100}; int temp1,temp2,number, end,i,j;scanf("%d",&number);end=a[9];if(number>end) 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];a[j]=temp1;temp1=temp2;}break;}}}for(i=0;i<11;i++)printf("%6d",a[i]);}7.5#define N 5main(){int a[N]={8,6,5,4,1},i,temp;for(i=0;i<N/2;i++){temp=a[i];a[i]=a[N-i-1];a[N-i-1]=temp;}for(i=0;i<N;i++)printf("%4d",a[i]);}7.6#define N 11main(){int i,j,a[N][N];for(i=1;i<N;i++){a[i][i]=1;a[i][1]=1;}for(i=3;i<N;i++)for(j=2;j<i;j++)a[i][j]=a[i-1][j-1]+a[i-1][j]; for(i=1;i<N;i++){for(j=1;j<=i;j++)printf("%6d",a[i][j]);printf("\n");}}7.7main(){int a[16][16],i,j,k,p,m,n;p=1;while(p==1){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++)a[i][j]=0;j=n/2+1;a[1][j]=1;for(k=2;k<=n*n;k++){i=i-1;j=j+1;if((i<1)&&(j>n)){i=i+2;j=j-1;}else{if(i<1)i=n;if(j>n)j=1;}if(a[i][j]==0)a[i][j]=k;else{i=i+2;j=j-1;a[i][j]=k;}}for(i=1;i<=n;i++){for(j=1;j<=n;j++)printf("%3d",a[i][j]);printf("\n");}}7.8#define N 10#define M 10main(){int i,j,k,m,n,flag1,flag2,a[N][M],max,maxi,maxj; scanf("%d,%d",&n,&m);for(i=0;i<n;i++)for(j=0;j<m;j++)scanf("%d",&a[i][j]);flag2=0;for(i=0;i<n;i++){max=a[i][0];for(j=0;j<m;j++)if(max<a[i][j]){max=a[i][j];maxj=j;}for(k=0,flag1=1;k<n&&flag1;k++)if(max>a[k][maxj])flag1=0;if(flag1){ printf("\na[%d][%d]=%d\n",i,maxj,max);flag2=1;}}if(!flag2) printf("NOT");}7.9#include<stdio.h>#define N 15main(){int i,j,number,top,bott,min,loca,a[N],flag;char c;for(i=0;i<=N;i++)scanf("%d",&a[i]);flag=1;while(flag){scanf("%d",&number);loca=0;top=0;bott=N-1;if((number<a[0])||(number>a[N-1]))loca=-1;while((loca==0)&&(top<=bott)){min=(bott+top)/2;if(number==a[min]){loca=min;printf("number=%d,loca=%d\n",number,loca+1); }else if(number<a[min])bott=min-1;elsetop=min+1;}if(loca==0||loca==-1)printf("%d not in table\n",number);printf("continue Y/N or y/n\n");c=getchar();if(c=='N'||c=='n')flag=0;}}7.10main(){int i,j,uppn,lown,dign,span,othn;char text[3][80];uppn=lown=dign=span=othn=0;for(i=0;i<3;i++){gets(text[i]);for(j=0;j<80&&text[i][j]!='\0';j++){if(text[i][j]>='A'&&text[i][j]<='Z')uppn++;else if(text[i][j]>='a'&&text[i][j]<='z') lown++;else if(text[i][j]>='0'&&text[i][j]<='9') dign++;else if(text[i][j]==' ')span++;elseothn++;}}for(i=0;i<3;i++)printf("%s\n",text[i]);printf("uppn=%d\n",uppn);printf("lown=%d\n",lown);printf("dign=%d\n",dign);printf("span=%d\n",span);printf("othn=%d\n",othn);}7.11main(){static char a[5]={'*','*','*','*','*'};int i,j,k;char space=' ';for(i=0;i<=5;i++){printf("\n");for(j=1;j<=3*i;j++)printf("%1c",space);for(k=0;k<=5;k++)printf("%3c",a[k]);}}7.12#include<stdio.h>main(){int i,n;char ch[80],tran[80];gets(ch);i=0;while(ch[i]!='\0'){if((ch[i]>='A')&&(ch[i]<='Z'))tran[i]=26+64-ch[i]+1+64;else if((ch[i]>='a')&&(ch[i]<='z'))tran[i]=26+96-ch[i]+1+96;elsetran[i]=ch[i];i++;}n=i;for(i=0;i<n;i++)putchar(tran[i]);}7.13main(){char s1[80],s2[40];int i=0,j=0;scanf("%s",s1);scanf("%s",s2);while(s1[i]!='\0')i++;while(s2[j]!='\0')s1[i++]=s2[j++];s1[i]='\0';printf("s=%s\n",s1);}7.14#include<stdio.h>main(){int i,resu;char s1[100],s2[100];gets(s1);gets(s2);i=0;while((s1[i]==s2[i])&&(s1[i]!='\0'))i++;if(s1[i]=='\0'&&s2[i]=='\0')resu=0;elseresu=s1[i]-s2[i];printf("s1=%s,s2=%s,resu=%d\n",s1,s2,resu); }7.15#include"stdio.h"main(){char from[80],to[80];;int i;scanf("%s",from);for(i=0;i<=strlen(from);i++) to[i]=from[i];printf("%s\n",to);}第八章8.1hcf(u,v)int u,v;{int a,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)int u,v,h;{return(u*v/h);}main(){int u,v,h,l;scanf("%d,%d",&u,&v);h=hcf(u,v);printf("H.C.F=%d\n",h);l=lcd(u,v,h);printf("L.C.D=%d\n",l);}8.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);q=sqrt(-disc)/(2*a);}main(){float a,b,c;scanf("%f,%f,%f",&a,&b,&c);disc=b*b-4*a*c;if(fabs(disc)<=1e-5){equal_to_zero(a,b);printf("x1=%5.2f\tx2=%5.2f\n",x1,x2);}else if(disc>0){greater_than_zero(a,b);printf("x1=%5.2f\tx2=%5.2f\n",x1,x2);}else{smaller_than_zero(a,b);printf("x1=%5.2f+%5.2fi\tx2=%5.2f-%5.2fi\n",p,q,p,q); }}8.3main(){int number;scanf("%d",&number);if(prime(number))printf("yes");elseprintf("no");}int prime(number)int number;{int flag=1,n;for(n=2;n<number/2&&flag==1;n++)if(number%n==0)flag=0;return(flag);}8.4#define N 3int array[N][N];convert(array)int array[3][3];{int i,j,t;for(i=0;i<N;i++)for(j=i+1;j<N;j++){t=array[i][j];array[i][j]=array[j][i];array[j][i]=t;}}main(){int i,j;for(i=0;i<N;i++)for(j=0;j<N;j++)scanf("%d",&array[i][j]);convert(array);for(i=0;i<N;i++){printf("\n");for(j=0;j<N;j++)printf("%5d",array[i][j]);}}8.5main(){char str[100];scanf("%s",str);inverse(str);printf("%s\n",str);}inverse(str)char str[];{char t;int i,j;for(i=0,j=strlen(str);i<strlen(str)/2;i++,j--) {t=str[i];str[i]=str[j-1];str[j-1]=t;}}8.6char concate(str1,str2,str)char str1[],str2[],str[];{int i,j;for(i=0;str1[i]!='\0';i++)str[i]=str1[i];for(j=0;str2[j]!='\0';j++)str[i+j]=str2[j];str[i+j]='\0';}main(){char s1[100],s2[100],s[100];scanf("%s",s1);scanf("%s",s2);concate(s1,s2,s);printf("\ns=%s",s);}8.7main(){char str[80],c[80];void cpy();gets(str);cpy(str,c);printf("\n%s\n",c);}void cpy(s,c)char s[],c[];{int i,j;for(i=0,j=0;s[i]!='\0';i++)if(s[i]=='a'||s[i]=='A'||s[i]=='e'||s[i]=='E'||s[i]=='i'|| s[i]=='I'||s[i]=='o'||s[i]=='O'||s[i]=='u'||s[i]=='U') {c[j]=s[i];j++;}c[j]='\0';}8.8main(){char str[80];scanf("%s",str);insert(str);}insert(str)char str[];{int i;for(i=strlen(str);i>0;i--){str[i*2]=str[i];str[i*2-1]=' ';}printf("%s\n",str);}8.9int alph,digit,space,others;main(){char text[80];gets(text);alph=0,digit=0,space=0,others=0;count(text);printf("\nalph=%d,digit=%d,space=%d,others=%d\n",alph,digit,space,oth ers);}count(str)char str[];{int i;for(i=0;str[i]!='\0';i++)if((str[i]>='a'&&str[i]<='z')||(str[i]>='A'&&str[i]<='Z'))alph++;else if(str[i]>='0'&&str[i]<='9')digit++;else if(strcmp(str[i],' ')==0)space++;elseothers++;}8.10int alph(c)char c;{if((c>='a'&&c<='z')||(c>='A'&&c<='Z'))return(1);elsereturn(0);}int longest(string)char string[];{int len=0,i,length=0,flag=1,place,point;for(i=0;i<=strlen(string);i++)if(alph(string[i]))if(flag){point=i;flag=0;}elselen++;else{flag=1;if(len>length){length=len;place=point;len=0;}}return(place);}main(){int i;char line[100];gets(line);for(i=longest(line);alph(line[i]);i++) printf("%c",line[i]);printf("\n");}8.11#define N 10char str[N];main(){int i,flag;for(flag=1;flag==1;){scanf("%s",str);if(strlen(str)>N)printf("input error");elseflag=0;}sort(str);for(i=0;i<N;i++)printf("%c",str[i]);}sort(str)char str[N];{int i,j;char t;for(j=1;j<N;j++)for(i=0;(i<N-j)&&(str[i]!='\0');i++) if(str[i]>str[i+1]){t=str[i];str[i]=str[i+1];str[i+1]=t;}}8.12#include<math.h>float solut(a,b,c,d)float a,b,c,d;{float x=1,x0,f,f1;do{x0=x;f=((a*x0+b)*x0+c)*x0+d;f1=(3*a*x0+2*b)*x0+c;x=x0-f/f1;}while(fabs(x-x0)>=1e-5);return(x);}main(){float a,b,c,d;scanf("%f,%f,%f,%f",&a,&b,&c,&d);printf("x=%10.7f\n",solut(a,b,c,d));}8.13main(){int x,n;float p();scanf("%d,%d",&n,&x);printf("P%d(%d)=%10.2f\n",n,x,p(n,x)); }float p(tn,tx)int tn,tx;{if(tn==0)return(1);else if(tn==1)return(tx);elsereturn(((2*tn-1)*tx*p((tn-1),tx)-(tn-1)*p((tn-2),tx))/tn); }8.14#define N 10#define M 5float score[N][M];float a_stu[N],a_cor[M];main(){int i,j,r,c;float h;float s_diff();float highest();r=0;c=1;input_stu();avr_stu();avr_cor();printf("\n number class 1 2 3 4 5 avr");for(i=0;i<N;i++){printf("\nNO%2d",i+1);for(j=0;j<M;j++)printf("%8.2f",score[i][j]);printf("%8.2f",a_stu[i]);}printf("\nclassavr");for(j=0;j<M;j++)printf("%8.2f",a_cor[j]);h=highest(&r,&c);printf("\n\n%8.2f %d %d\n",h,r,c);printf("\n %8.2f\n",s_diff());}input_stu(){int i,j;float x;for(i=0;i<N;i++){for(j=0;j<M;j++){scanf("%f",&x);score[i][j]=x;}}}avr_stu(){int i,j;float s;for(i=0;i<N;i++){for(j=0,s=0;j<M;j++)s+=score[i][j];a_stu[i]=s/5.0;}}avr_cor(){int i,j;float s;for(j=0;j<M;j++){for(i=0,s=0;i<N;i++)s+=score[i][j];a_cor[j]=s/(float)N;}}float highest(r,c)int *r,*c;{float high;int i,j;high=score[0][0];for(i=0;i<N;i++)for(j=0;j<M;j++)if(score[i][j]>high){high=score[i][j];*r=i+1;*c=j+1;}return(high);}float s_diff(){int i,j;float sumx=0.0,sumxn=0.0;for(i=0;i<N;i++){sumx+=a_stu[i]*a_stu[i];sumxn+=a_stu[i];}return(sumx/N-(sumxn/N)*(sumxn/N)); }8.15#include<stdio.h>#define N 10void input_e(num,name)int num[];char name[N][8];{int i;for(i=0;i<N;i++){scanf("%d",&num[i]);gets(name[i]);}}void sort(num,name)int num[];char name[N][8];{int i,j,min,temp1;char temp2[8];for(i=0;i<N-1;i++){min=i;for(j=i;j<N;j++)if(num[min]>num[j])min=j;temp1=num[i];num[i]=num[min];num[min]=temp1;strcpy(temp2,name[i]);strcpy(name[i],name[min]);strcpy(name[min],temp2);}for(i=0;i<N;i++)printf("\n%5d%10s",num[i],name[i]);}void search(n,num,name)int n,num[];char name[N][8];{int top,bott,min,loca;loca=0;top=0;bott=N-1;if((n<num[0])||(n>num[N-1]))loca=-1;while((loca==0)&&(top<=bott)){min=(bott+top)/2;if(n==num[min]){loca=min;printf("number=%d,name=%s\n",n,name[loca]); }else if(n<num[min])bott=min-1;elsetop=min+1;}if(loca==0||loca==-1)printf("number=%d is not in table\n",n);}main(){int num[N],number,flag,c,n;char name[N][8];input_e(num,name);sort(num,name);for(flag=1;flag;){scanf("%d",&number);search(number,num,name);printf("continue?Y/N!");c=getchar();if(c=='N'||c=='n')flag=0;}}8.16#include<stdio.h>#define MAX 1000main(){int c,i,flag,flag1;char t[MAX];i=0;flag=0;flag1=1;while((c=getchar())!='\0'&&i<MAX&&flag1){if(c>='0'&&c<='9'||c>='A'&&c<='F'||c>='a'&&c<='f') {flag=1;t[i++]=c;}else if(flag){t[i]='\0';printf("\nnumber=%d\n",htoi(t));printf("continue?");c=getchar();if(c=='n'||c=='N')flag1=0;else{flag=0;i=0;}}}}htoi(s)char s[];{int i,n;n=0;for(i=0;s[i]!='\0';i++){if(s[i]>='0'&&s[i]<='9')n=n*16+s[i]-'0';if(s[i]>='a'&&s[i]<='f')n=n*16+s[i]-'a'+10;if(s[i]>='A'&&s[i]<='F')n=n*16+s[i]-'A'+10;}return(n);}8.17#include<stdio.h>void convert(n)int n;{int i;if((i=n/10)!=0)convert(i);putchar(n%10+'0');}main(){int number;scanf("%d",&number);if(number<0){putchar('-');number=-number;}convert(number);}8.18main(){int year,month,day;int days;scanf("\n%d,%d,%d",&year,&month,&day);。
C语言程序设计第三版谭浩强课后习题答案解析[完整版]
1.5#include <stdio.h>void main(){printf("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n");printf(" Very good! \n");printf("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n");}1.6#include <stdio.h>void main(){float max(float x,float y,float z);float a,b,c;printf("请分别输入a,b,c:\n");scanf("%f,%f,%f",&a,&b,&c);printf("max=%f",max(a,b,c));}float max(float x,float y,float z){float m,n;m=x>y? x: y;n=m>z? m: z;return(n);}3.6#include <stdio.h>void main(){char c1=’a’,c2=’b’,c3=’c’,c4=’\101’,c5=’\116’;printf(“a%c b%c\tc%c\tabc\n”,c1,c2,c3);printf(“\t\b%c %c\n”,c4,c5);}3.9.1#include <stdio.h>void main(){double x=2.5,y=4.7,z;int a=7;z=x+a%3*(int)(x+y)%2/4;printf("该表达式的值为:%f",z);}3.9.2#include <stdio.h>void main(){int a=2,b=3;float x=3.5,y=2.5,z;z=(float)(a+b)/2+(int)x%(int)y;printf("该表达式的值为:%f",z);}4.5#include <stdio.h>void main(){int a=5,b=7;double x=67.8564,y=-789.124;char c='A';long n=1234567;unsigned u=65535;printf("%d%d\n",a,b);printf("%3d%3d\n",a,b);printf("%f,%f\n",x,y);printf("%-10f,%-10f\n",x,y);printf("%8.2f,%8.2f,%.4f,%.4f,%3f,%3f\n",x,y,x,y,x,y);printf("%e,%10.2e\n",x,y);printf("%c,%d,%o,%x\n",n,n,n);printf("%ld,%lo,%x\n",n,n,n);printf("%u,%o,%x,%d\n",u,u,u,u);printf("%s,%5.3s\n","COMPUTER","COMPUTER"); }4.6#include <stdio.h>void 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);}4.7#include<stdio.h>void main(){int a,b;float x,y,z;char c1,c2;scanf("%5d%5d",&a,&b);scanf("%f%f%*f%f",&x,&y,&z);scanf("%c%c",&c1,&c2);}4.8#include <stdio.h>#define pi 3.14void main(){float r,h,l,s1,s2,v1,v2;printf("请输入r,h的值:\n");scanf("%f,%f",&r,&h);l=2*pi*r;s1=pi*r*r;s2=4*s1;v1=4/3*pi*r*r*r;v2=s1*h;printf("圆周长为:%.2f\n",l);printf("圆面积为:%.2f\n",s1);printf("圆球表面积为:%.2f\n",s2); printf("圆球体积为:%.2f\n",v1); printf("圆柱体积为:%.2f\n",v2);}5.5 #include <stdio.h>void main(){float x,y;printf("请输入x的值:\n");scanf("%f",&x);if(x<1)y=x;elseif(x>=1&&x<10)y=2*x-1;elsey=3*x-11;printf("x与y的值分别为:\n%f,%f",x,y); printf("\n");}5.6 #include <stdio.h>void main(){float b;int a;printf("请输入该同学的成绩:\n");scanf("%f",&b);while (b<0||b>100){printf("输入有误,请重新输入:\n");scanf("%f",&b);}a= int(b/10);if(a<6) a=5;switch(a){case 10:case 9: printf("该同学为等级:A");break;case 8: printf("该同学为等级:B");break;case 7: printf("该同学为等级:C");break;case 6: printf("该同学为等级:D");break;case 5: printf("该同学为等级:E");break;}}#include <stdio.h>#include <math.h>void main(){int x,y,m,n,i=1,j=0;printf("请给出一个不多于5位的正整数:\n");scanf("%d",&x);while(x<=0||x>=100000){ printf("输入有误\n请给出一个不多于5位的正整数:\n");scanf("%d",&x);}for(n=0;n<5;n++){y=int(x/pow(10,n));if(y>0&&y<10)m=n+1;}printf("它是%d位数\n",m);printf("从高到低位依次为\n");do{printf(" %d",int(x/pow(10,m-i))%10);i++;}while(m-i>=0);printf("\n逆序输出为\n");do{printf(" %d",int(x/pow(10,j))%10);j++;}while(j-m<0);printf("\n");}6.1输入两个正整数m和n,求其最大公约数和最小公倍数。
c语言程序设计第三版谭浩强课后习题答案
c语言程序设计第三版谭浩强课后习题答案【篇一:c语言程序设计第三版谭浩强课后习题答案完整版】ude stdio.hvoid main(){printf(* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n);printf(very good! \n); printf(* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n);}1.6#include stdio.hvoid main(){float max(float x,float y,float z);float a,b,c;printf(请分别输入a,b,c:\n);scanf(%f,%f,%f,a,b,c);printf(max=%f,max(a,b,c));}float max(float x,float y,float z){float m,n;m=xy? x: y;n=mz? m: z;return(n);}3.6#include stdio.hvoid main(){char c1=’a’,c2=’b’,c3=’c’,c4=’\101’,c5=’\116’;printf(“a%c b%c\tc%c\tabc\n”,c1,c2,c3);printf(“\t\b%c %c\n”,c4,c5);}3.9.1#include stdio.hvoid main(){double x=2.5,y=4.7,z;int a=7;z=x+a%3*(int)(x+y)%2/4;printf(该表达式的值为:%f,z);}3.9.2#include stdio.hvoid main(){int a=2,b=3;float x=3.5,y=2.5,z;z=(float)(a+b)/2+(int)x%(int)y;printf(该表达式的值为:%f,z);}4.5#include stdio.hvoid main(){int a=5,b=7;double x=67.8564,y=-789.124;char c=a;long n=1234567;unsigned u=65535;printf(%d%d\n,a,b);printf(%3d%3d\n,a,b);printf(%f,%f\n,x,y);printf(%-10f,%-10f\n,x,y);printf(%8.2f,%8.2f,%.4f,%.4f,%3f,%3f\n,x,y,x,y,x,y); printf(%e,%10.2e\n,x,y);printf(%c,%d,%o,%x\n,n,n,n);printf(%ld,%lo,%x\n,n,n,n);printf(%u,%o,%x,%d\n,u,u,u,u);printf(%s,%5.3s\n,computer,computer);}4.6#include stdio.hvoid 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);}4.7#includestdio.hvoid main(){int a,b;float x,y,z;char c1,c2;scanf(%5d%5d,a,b);scanf(%f%f%*f%f,x,y,z);scanf(%c%c,c1,c2);}4.8#include stdio.h#define pi 3.14void main(){float r,h,l,s1,s2,v1,v2;printf(请输入r,h的值:\n);scanf(%f,%f,r,h);l=2*pi*r;s1=pi*r*r;s2=4*s1;v1=4/3*pi*r*r*r;v2=s1*h;printf(圆周长为:%.2f\n,l);printf(圆面积为:%.2f\n,s1);printf(圆球表面积为:%.2f\n,s2); printf(圆球体积为:%.2f\n,v1); printf(圆柱体积为:%.2f\n,v2); }5.5 #include stdio.hvoid main(){float x,y;printf(请输入x的值:\n);scanf(%f,x);if(x1)y=x;elseif(x=1x10)y=2*x-1;elsey=3*x-11;printf(x与y的值分别为:\n%f,%f,x,y); printf(\n);}5.6 #include stdio.hvoid main(){float b;int a;printf(请输入该同学的成绩:\n);scanf(%f,b);while (b0||b100){printf(输入有误,请重新输入:\n);scanf(%f,b);}a= int(b/10);if(a6) a=5;switch(a){case 10:case 9: printf(该同学为等级:a);break; case 8: printf(该同学为等级:b);break; case 7: printf(该同学为等级:c);break; case 6: printf(该同学为等级:d);break; case 5: printf(该同学为等级:e);break; }}#include stdio.h#include math.hvoid main(){int x,y,m,n,i=1,j=0;printf(请给出一个不多于5位的正整数:\n);scanf(%d,x);while(x=0||x=100000){printf(输入有误\n请给出一个不多于5位的正整数:\n);scanf(%d,x);}for(n=0;n5;n++){y=int(x/pow(10,n));【篇二:c语言程序设计+谭浩强+第三版+课后习题答案】> **************************very good!**************************解:mian(){printf(“**************************”);printf(“\n”);printf(“very good!\n”);printf(“\n”);printf(“**************************”);}1.6 编写一个程序,输入a、b、c三个值,输出其中最大值。
(完整word版)C语言程序设计(第3版)谭浩强课后习题答案(可编辑修改word版)
1.1请参照本章例题,编写一个c程序,输出以下信息:廿**廿***甘**廿**脊*并********甘**甘**Very good!甘**甘**************************解:程序如下所示。
番include <stdio.h> int main ()( printf ("**************************\n\n"); printf (" Very Good!\n\n");printf ("**************************\n n); return 0;运行结果如下:****************************** Very good!#******#**#******#**#***#**#**1.2编写一个C程序,输入a、b、c3个值,输出其中最大者。
解:程序如下所示。
番include <stdio.h>int main(){int a,b, c,max;printf ("please input a,b,c: \n");scanf %d",&a,Sb/ &c);max=a;if (max<b)max=b;if (max<c)max=c;printf ("The largest number is % d\n*\max);return 0;运行结果如下:please input a,b,c:38,93,-84The largest number is 931.3上机运行本章3个例题,熟悉所用系统的上机方法与步骤。
解:略。
1.4上机运行你为本章习题1.1和4题1.2所编写的程序。
解:略。
2. 1假如我国国民生产总值的年增长率为10%,汁界10年后我国国民生产总值与现在相比增长多少。
汁箅公式为:P=(l + r)n「为年增长率,〃为年数,P为与现在相比的百分比。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
1. 5请参照本章例题,编写一个C程序,输出以下信息:**************************Very Good!**************************解:mian(){printf(“**************************”);printf(“\n”);printf(“Very Good!\n”);printf(“\n”);printf(“**************************”);}1.6 编写一个程序,输入a、b、c三个值,输出其中最大值。
解:mian(){int a,b,c,max;printf(“请输入三个数a,b,c:\n”);scanf(“%d,%d,%d”,&a,&b,&c);max=a;if(max<b)max=b;if(max<c)max=c;printf(“最大数为:“%d”,max);}第三章3.6写出以下程序运行的结果。
main(){char c1=’a’,c2=’b’,c3=’c’,c4=’\101’,c5=’\116’;printf(“a%cb%c\tc%c\tabc\n”,c1,c2,c3);printf(“\t\b%c %c”,c4,c5);}解:aaㄩbbㄩㄩㄩccㄩㄩㄩㄩㄩㄩabcAㄩN3.7 要将"China"译成密码,译码规律是:用原来字母后面的第4个字母代替原来的字母.例如,字母"A"后面第4个字母是"E"."E"代替"A"。
因此,"China"应译为"Glmre"。
请编一程序,用赋初值的方法使cl、c2、c3、c4、c5五个变量的值分别为,’C’、’h’、’i’、’n’、’a’,经过运算,使c1、c2、c3、c4、c5分别变为’G’、’l’、’m’、’r’、’e’,并输出。
解:#include <stdio.h>main(){ char c1=’C’,c2=’h’,c3=’i’,c4=’n’,c5=’a’;c1+=4;c2+=4;c3+=4;c4+=4;c5+=4;printf("密码是%c%c%c%c%c\n",c1,c2,c3,c4,c5);}运行结果:密码是Glmre3.9求下面算术表达式的值。
(1)x+a%3*(int)(x+y)%2/4设x=2.5,a=7,y=4.7(2)(float)(a+b)/2+(int)x%(int)y设a=2,b=3,x=3.5,y=2.5(1)2.5(2)3.53.10写出程序运行的结果。
main(){int i,j,m,n;i=8;j=10;m=++i;n=j++;printf(“%d,%d,%d,%d”,i,j,m,n);}解:9,11,9,103.12 写出下面表达式运算后a的值,设原来a=12。
设a和n都已定义为整型变量。
(1)a+=a (2) a-=2 (3) a*=2+3 (4)a/=a+a(5) a%=(n%=2),n的值等于5(6)a+=a-=a*=a解:(1) 24 (2) 10 (3) 60 (4) 0 (5) 0 (6) 0第四章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=_3_ _b=_4_ _c=_5x=1.200000,y=2.400000,z=-3.600000x+y=_3.600_ _y+z=-1.20_ _z+x=-2.40c1=ˊaˊ_or_97(ASCII)c2=ˊbˊ_or_98(ASCII)main(){int a=3,b=4,c=5;long int u=51274,n=128765;float x=1.2,y=2.4,z=3.6;char c1=’a’,c2=’b’;printf("a=%2d b=%2d c=%2d\n",a,b,c);printf("x=%f,y=%f,z=%f\n",x,y,z);printf("x+y=%5.2f y+z=%5.2f z+x=%5.2f\n",x+y,y+z,z+x);printf("u=%6ld n=%9ld\n",u,n);printf("%s %s %d%s\n","c1=’a’","or",c1,"(ASCII)");printf("%s %s %d%s\n","c2=’a’","or",c2,"(ASCII)");}4.7用scanf下面的函数输入数据,使a=3,b=7,x=8.5,y=71.82,c1=ˊAˊ,c2=ˊaˊ,问在键盘上如何输入?main(){int a,b;float x,y;char c1c2;scanf("a=%d_b=%d",&a,&b);scanf("_x=%f_y=%e",&x,&y);scanf("_c1=%c_c2=%c",&c1,&c2);}a=3_b=7_x=8.5_y=71.82_c1=A_c2=a4.8设圆半径r=1.5,圆柱高h=3,求圆周长、圆面积、圆球表面积、圆球体积、圆柱体积。
用scanf输入数据,输出计算结果,输出时要求文字说明,取小数点后两位数字。
请编程序。
main(){float r,h,C1,Sa,Sb,Va,Vb;scanf("%f,%f",&r,&h);C1=2*3.14*r;Sa=3.14*r*r;Sb=4*Sa;Va=4*3.14*r*r*r/3;Vb=Sa*h;printf("C1=%.2f\n",C1);printf("Sa=%.2f\nSb=%.2f\nVa=%.2f\nVb=%.2f\n",Sa,Sb,Va,Vb);}4.9输入一个华氏温度,要求输出摄氏温度。
公式为c=5(F-32)/9输出要求有文字说明,取位2小数。
main(){float F,c;scanf("%f",&F);c=5*(F-32)/9;printf("c=%.2f",c);}4.10编程序,用getchar函数读入两个字符给c1、c2,然后分别用函数和函数输出这两个字符。
并思考以下问题:(1)变量c1、c2应定义为字符型或整形?抑二者皆可?(2)要求输出c1和c2值的ASCII码,应如何处理?用putchar函数还是printf函数?(3)整形变量与字符变量是否在任何情况下都可以互相代替?如:char c1,c2;与int c1,c2;是否无条件的等价?#include"stdio.h"main(){char c1,c2;c1=getchar();c2=getchar();putchar(c1);putchar(’\n’);putchar(c2);putchar(’\n’);}#include"stdio.h"main(){char c1,c2;c1=getchar();c2=getchar();printf("c1=%d c2=%d\n",c1,c2);printf("c1=%c c2=%c\n",c1,c2);}第五章5.1 什么是算术运算?什么是关系运算?什么是逻辑运算?解:略。
5.2 C语言中如何表示“真”和“假”?系统如何判断一个量的“真”和“假”?解:设有一个逻辑表达式,若其结果为“真”,则以1表示;若其结果为“假”,则以0表示。
但是判断一个逻辑量的值时,以0代表“真”,以非0代表“假”。
例如3&&5的值为“真”,系统给出3&&5的值为1。
5.3 写出下面各逻辑表达式的值。
设a=3,b=4,c=5。
(1) a+b>c&&b==c(2) a||b+c&&b-c(3) !(a>b)&&!c||1(4) !(x=a)&&(y=b)&&0(5) !(a+b)+c-1&&b+c/2解:(1) 0(2) 1(3) 1(4) 0(5) 15.4 有3个整数a、b、c,由键盘输入,输出其中最大的数。
解:方法一#include <stdio.h>main(){ int a,b,c;printf("请输入3个整数:");scanf("%d,%d,%d",&a,&b,&c);if(a<b)if(b<c) printf("max=%d\n",c);else printf("max=%d\n",b);else if(a<c) printf("max=%d\n",c);else printf("max=%d\n",a);}方法二:使用条件表达式,可以使程序更加简明、清晰。
程序如下:#include <stdio.h>main(){ int a,b,c,temp,max;printf("请输入3个整数:");scanf("%d,%d,%d",&a,&b,&c);temp=(a>b)?a:b; /* 将a和b中的大者存人temp中 */max=(temp>c)?temp:c; /* 将a和b中的大者与c比较,取最大者*/ printf("3个整数的最大数是%d\n”,max);}5.5 有一函数:写一程序,输入x值,输出y值。
解:#include <stdio.h>main(){int x,y;printf("输入x:");scanf("%d",&x);if(x<1) /* x<1 */{ y=x;printf("x=%3d, y=x=%d\n",x,y);}else if (x<10) /* 1≤x-10 */{ y=2*x-1;printf("x=%3d, y=2*x-1=%d\n",x,y);}else /* x≥10 */{ y=3*x-11;printf("x=%3d, y=3*x-11=%d\n",x,y);}}5.6 给一个百分制成绩,要求输出等级’A’、’B’、’C’、’D’、’E’。