C语言程序设计教程李含光郑关胜清华大学出版社习题答案习题答案完美 .doc
第1章习题参考答案1单项选择题(1)A (2)C (3)D (4)C (5)B2填空题(1)函数(2)主函数(main)(3)printf() , scanf() 第2章习题参考答案1单项选择题1-5 CBCCC 6-10 CDCDC 11-13 DBB2填空题(1)1 (2)26 (3)6 , 4 , 2 (4)10 , 6(5)3.000000(6)双精度(double)(7)9 (8)字母 ,数字 ,下划线(9)13.700000(10)11(11)(m/10)%10)*100+(m/100)*10+m%10(12)0(13)10 ,9 ,11(15)(x<0&&y<0)|(x<0&&z<0)|(y<0|z<0)(16)double(17)x=0(18)sqrt(fabs(a-b)/(3*(a+b)(19)sqrt(x*x+y*y)/(a+b)第3章习题参考答案1单项选择题1-5 CCCDD 6-10 BCDBC 11-15 BCBBB 16 A2填空题(1)用;表示结束(2) (3)y=x<0?1:x=0?0:-1(4)y%4=0&&y%100!=0|y%400=0(5)上面未配对(6)default标号(7)while , do while , for(8)do while(9)本次(10)本层3阅读程序,指出结果(1)yes(2)*&(3)ABother(4)28 70(5)2,0(6)8(7)36(8)1(9)3,1,-1,3,1,-1(10)a=12 ,y=12(11)i=6,k=4(12)1,-24程序填空(1)x:y , u:z(2)m=n , m!=0 ,m=m/10(3)t<eps , t*n/(2*n+1) , printf(“%lfn”,2*s)(4)m%5=0 , printf(“%dn”,k)(5)cx=getchar() , cx!=front , cx(6)double s=0 , 1.0/k , %lf(7)s>=0 , s<gmin ,5编程题(1)#include <stdio.h>int main() double x,y; scanf("%lf",&x); if(x<1) y=x; else if(x>=1.0&&x<10) y=2*x-11; else y=3*x-11; printf("%lfn",y); return 0;(2)#include <stdio.h>int main() double x,y,z,min; scanf("%lf%lf%lf",&x,&y,&z); if(x>y) min=y; else min=x; if(min>z) min=z; printf("min=%lfn",min); return 0;(3)#include <stdio.h>int main() int y,m,d,flag,s=0,w,i; scanf("%d%d%d",&y,&m,&d); flag=(y%4=0&&y%100!=0|y%400=0); w=(y-1)*365+(y-1)/4-(y-1)/100+(y-1)/400)%7; for(i=1;i<=m;i+) switch(i) case 1:s=d;break; case 2:s=31+d;break; case 3:s=59+d;break; case 4:s=90+d;break; case 5:s=120+d;break; case 6:s=151+d;break; case 7:s=181+d;break; case 8:s=212+d;break; case 9:s=243+d;break; case 10:s=273+d;break; case 11:s=304+d;break; case 12:s=334+d;break; if(flag=1&&m>2) s=s+1; s=(w+s)%7; if(s=0) printf("星期日n"); else printf("星期%dn",s); return 0;(4)#include <stdio.h>int main() float p,r; scanf("%f",&p); if(p<=10) r=p*0.1; else if(p>10&&p<=20) r=10*0.1+(p-10)*0.075; else if(p>20&&p<=40) r=10*0.1+10*0.075+(p-20)*0.05; else if(p>40&&p<=60) r=10*0.1+10*0.075+20*0.05+(p-40)*0.03; else if(p>60&&p<=100) r=10*0.1+10*0.075+20*0.05+20*0.03+(p-60)*0.015; else if(p>100) r=10*0.1+10*0.075+20*0.05+20*0.03+40*0.015+(p-100)*0.01; printf("%fn",r); return 0;(5)#include <stdio.h>int main() char c; while(c=getchar()!='n') if(c>='a'&&c<='z') c=c-32; putchar(c); return 0;(6)#include<stdio.h>int main() int m,k=2; printf("输入一个正整数:n"); scanf("%d",&m); while(k<m) if(m%k=0) printf("%4d",k); m=m/k; else k+; printf("%4dn",m); return 0;(7)#include<stdio.h>int main() int a,n,s=0,p=0,i; scanf("%d %d",&n,&a); for(i=1;i<=n;i+) p=p*10+a; s=s+p; printf("%dn",s); return 0;(8)#include<stdio.h>int main() int i,j,k; for(i=1;i<=9;i+) for(j=0;j<=9;j+) for(k=0;k<=9;k+) printf("%5d",100*i+10*j+k); return 0;(9)#include<stdio.h>#include<math.h>int main() float a=-10,b=10,x,f1,f2,f; f1=(2*a-4)*a+3)*a)-6; f2=(2*b-4)*b+3)*b)-6; do x=(a+b)/2; f=(2*x-4)*x+3)*x)-6; if(f*f1<0) b=x; f2=f; else a=x; f1=f; while(fabs(f)>=1e-6); printf("%6.2fn",x); return 0;(10)#include<stdio.h>#include<math.h>int main() int n=2; double eps,t,s=0,x; scanf("%lf %lf",&x,&eps); t=x; s=t; while(fabs(t)>=eps) t=-t*(2*n-3)*x*x/(2*n-2); s=s+t/(2*n); n+; printf("%d,%lfn",n,s); return 0;(11)#include<stdio.h>int main() unsigned long s,t=0,p=1; scanf("%u",&s); while(s!=0) if(s%10)%2!=0) t=t+(s%10)*p; p=p*10; s=s/10; printf("%un",t); return 0;第4章习题参考答案1单项选择题1-5 DDDBD 6-10 BADCD 11-14 BDAB2填空题(1)2(2)嵌套 , 递归(3)全局变量 , 局部变量 , 静态变量 , 动态变量(4)auto , static , register , extern(5)外部变量(6)编译 ,运行3阅读程序,指出结果(1)15(2)5(3)5,4,3(4)i=5 i=2 i=2 i=4 i=2(5)求水仙花数(6)-5*5*5(7)30(8)0 10 1 11 2 124程序填空(1)float fun(float , float) , x+y,x-y , z+y,z-y(2)x , x*x+1(3)s=0 , a=a+b5编程题(1)#include<stdio.h>unsigned int fun(unsigned int);int main() unsigned int s; scanf("%u",&s); printf("%un",fun(s); return 0;unsigned int fun(unsigned int s) unsigned int p=0; while(s!=0) p=p+s%10; s=s/10; return p;(2)#include<stdio.h>#include<stdlib.h>#include<math.h>void f1(float,float,float,float);void f2(float,float,float,float);void f3(float,float,float,float);int main() float a,b,c,d; scanf("%f %f %f",&a,&b,&c); if(a=0) printf("不是一元二次方程n"); exit(0); d=b*b-4*a*c; if(d>0) f1(a,b,c,d); else if(d=0) f2(a,b,c,d); else f3(a,b,c,d); return 0;void f1(float a,float b,float c,float d) float x1,x2; x1=(-b+sqrt(d)/(2*a); x2=(-b-sqrt(d)/(2*a); printf("%.2f ,%.2fn",x1,x2); void f2(float a,float b,float c,float d) float x1,x2; x1=-b/(2*a); x2=-b/(2*a); printf("%.2f ,%.2fn",x1,x2); void f3(float a,float b,float c,float d) float x1,x2; x1=-b/(2*a); x2=sqrt(-d)/(2*a); printf("%.2f+i*%.2fn",x1,x2); printf("%.2f-i*%.2fn",x1,x2); (3).#include<stdio.h>double p(int,double);int main() int n; double x; do scanf("%d",&n); while(n<0); scanf("%lf",&x); printf("%lfn",p(n,x); return 0;double p(int n,double x) double pn; if(n=0) pn=1; else if(n=1) pn=x; else pn=(2*n-1)*x*p(n-1,x)-(n-1)*p(n-2,x)/n; return pn;(4)#include<stdio.h>#define RATE 0.053double fun(float);void display(float,int);int main() float dep; int season; scanf("%f %d",&dep,&season); display(dep,season); return 0;double fun(float d) return d*RATE;void display(float d,int s) int i; printf("季度 利 余额n"); printf("-n"); for(i=1;i<=s;i+) printf("%-4d %-.2f %-.2fn",i,fun(d),fun(d)*i+d); printf("-n"); (5)#include<stdio.h>double fun(void);int main() printf("%lfn",fun(); return 0;double fun(void) double s=0; int n=1; while(double)(2*n-1)/(2*n)*(2*n)>1e-4) s=s+(double)(2*n-1)/(2*n)*(2*n); n+; return s;(6)#include<stdio.h>int fun(int);int main() int w; scanf("%d",&w); printf("%dn",fun(w); return 0;int fun(int w) int n=1,p=1,m; m=w; while(m>10) m=m/10; p=p*10; n+; return w%p;第5章习题参考答案1、选择题:1-5 C (B C) BBA 6-8 DDB2、填空题(1) 0,9(2) float realArray100,char strArray16,int intArray1000(3) 运算符 sizeof(4) 6字节3、阅读程序,写出下面程序的运行结果(1) aa bb cc dd(2) ab c d(3)ahAMa(4)0010(5) 1 3 4 5(6)This is the title. Name 1 is Rosalinda Name 2 is Zeke The biggest name alpabetically is Zeke Both names are Rosalinda Zeke(7)0 0 0 0 0 0 0 0 0 1 2 3 4 5 6 7 0 2 5 6 8 10 748 14 0 3 6 9 12 15 18 21 0 4 8 12 16 20 24 28 0 5 10 15 20 177 30 35 0 6 12 18 24 30 36 42 0 7 14 21 28 35 42 494、程序填空(1) aij != aji , 1(2) 0, ai < amini , maxi = i , amaxi = amini(3) int a, int b, bi = ai, -999, arraycopy(a,b), bi+(4) a<sizeof(ch), if5、编程题(1)#include<stdio.h>int main ( ) 1 int a34 = 0, 2,9,7, 5,13,6,8, 27,11,1,3 ; int i,j,temp; for(i=0,j=0;j<4;j+) temp = a2-ij; a2-ij = aij; aij = temp; for(i=0;i<3;i+) for(j=0;j<4;j+) printf("%3d",aij); printf("n"); return 0;(2)#include<stdio.h>int main ( ) static int a66; int i,j,t=1; for(i=0;i<6;i+) t = i+1; for(j=0;j<i+1;j+) aij = t-; for(i=0;i<6;i+) for(j=0;j<6;j+) printf("%3d",aij); printf("n"); return 0;(3)#include <stdio.h>#define M 3#define N 4#define R 5int main( ) static int aMN,bNR,cMR; int i,j,k; 2 printf("Matrix a:n"); for( i = 0; i < M; i+ ) for( j = 0; j < N; j+ ) scanf( "%d",&aij ); printf("Matrix b:n"); for( i = 0; i < N; i+ ) for( j = 0; j < R; j+ ) scanf( "%d",&bij ); for( i = 0; i < M; i + ) for( j = 0; j < N; j+ ) for( k = 0; k < R; k+) cik += aij*bjk; for( i = 0; i < M; i+ ) for( j = 0; j < R; j+ ) printf( "%3d",cij ); printf("n"); return 0;(4)#include <stdio.h>#define M 5int main( ) static int aM; int i,max = -1,min = 100,maxi,mini,temp; for( i = 0; i < M; i+ ) scanf( "%d",&ai ); /099间的值 for( i = 0; i < M; i+ ) if( max < ai ) max = ai; maxi = i; if( min > ai ) min = ai; mini = i; temp = amaxi; amaxi = amini; amini = temp; for( i = 0; i < M; i+ ) printf( "%3d",ai ); return 0; 3(5)#include <stdio.h>#define M 3#define N 4int main( ) static int aMN; int max = -1,sumcol=0,sumrow=0; int i,j,maxi,col; for( i = 0; i < M; i+ ) sumrow = 0; for( j = 0; j < N; j+ ) scanf( "%d",&aij ); sumrow += aij; if ( max < sumrow ) max = sumrow; maxi = i; printf("which col sum will be caculated?(>0)"); scanf("%d",&col); for( i = 0; i < M; i + ) sumcol += aicol-1; printf("The %dth row's sum is max, max=%dn",maxi+1,max); printf("The %dth column's sum=%dn",col,sumcol); return 0;(6)#include <stdio.h>#define M 81int main( ) static char strM; int i,count = 0; char ch; gets(str); fflush(stdin); /清空输入缓冲区,以便读入字符数据 printf("Which character will be found?"); ch = getchar(); for( i = 0; i < strlen(str); i+ ) if( ch = stri ) count+; printf( "The number of character '%c' is %dn",ch,count ); 4 return 0;(7)#include <stdio.h>#include <stdlib.h>#define N 10 /同学人数#define M 5 /课程数void enter_scores(void);void sort_scores(int scoreM, int averageN3);void disp_scores(int scoreM);void histogram(int n, int scoreM);void printchar(int n);static int scoreNM;static int averN3; /第1列为均值,第2列原始顺序,第3列为均值逆序int main() int course; enter_scores(); printf("n=Oringenal Score Start=n"); disp_scores(score); printf("n=Oringenal Score End=n"); printf("n=Sorted Score Start=n"); sort_scores(score,aver); printf("n=Sorted Score End=n"); printf("nWhich class will be statisticed?n"); scanf("%d",&course); histogram(course, score); system("Pause"); return 0;/* 输入成绩 */void enter_scores() int i, j; for(i=0; i<N; i+) for(j=0; j<M; j+) scanf("%d",&scoreij); averi0 += scoreij; for( i = 0; i < N; i+ ) for ( j = 1; j < 3; j+ ) 5 averij = i;/* 成绩排序. */void sort_scores(int scoreM, int averageN3) int i,j,t; int temp,tempindex; for( i = 0; i < N - 1; i+ ) for( j = 0; j < N - 1 - i; j+ ) if ( averagej0 < averagej+10 ) temp = averagej0; averagej0 = averagej+10; averagej+10 = temp; tempindex = averagej2; averagej2 = averagej+12; averagej+12 = tempindex; printf("n Score1 Score2 Score3 Score4 Score5n"); for( i = 0; i < N; i+ ) t = averagei2; for( j = 0;j < M; j+ ) printf("%8d", scoretj); printf("n"); /* 输出成绩 */void disp_scores(int scoreM) int i, j; printf("n Score1 Score2 Score3 Score4 Score5n"); for( i = 0; i < N; i+ ) for( j = 0;j < M; j+ ) printf("%8d", scoreij); printf("n"); void printchar(int n) 6 int i; for (i = 0; i < n; +i) putchar('*');void histogram(int course, int scoreM) int i,temp; int segs5 = 0; int scN; for( i = 0; i < N; i+ ) sci = scoreicourse; for (i = 0; i < N; i+)/* 统计各分段人数 */ temp = (int)sci/10; segs temp<6?0:temp-5+; /* 成绩/10,将成绩分段 */ printf("nSegment Numbern"); for (i = 0; i < 5; i+) /* 输出直方图,每段的人数 */ printf("<%3d: %5d| ", (i+1)*10+50, segsi); printchar(segsi); putchar('n'); putchar('n');(8)#include <stdio.h>#define M 81int main( ) static char strM; int i,count = 0; char ch = 0; gets(str); for( i = 0; i < strlen(str); i+ ) if( ch < stri ) ch = stri; for( i = 0; i < strlen(str); i+ ) if( ch = stri ) count+; printf( " max = '%c' ,count = %dn",ch,count ); return 0;7第6章习题参考答案1、选择题:1-5 BBDDA 6-10 CCABD 11-15 CBCDA 16 A2、填空题(1) 常量,变量(2) 指针(3)2(4)12,12(5) a0,a33、阅读程序,写出下面程序的运行结果(1)5(2)6(3) abcdefglkjih(4) 976531(5)5,9(6)2,4,5,7,6,0,11,9,7,3,(7)string_a=I am a teacher. string_b=You are a student. string_a=I am a teacher. string_b=I am a teacher.4、程序填空(1) *p != '0', *p-'0', j(2) i <strlen(str), j=i, k+1(3)a+i, (char)(n%10) + '0'(4)*pk = i, a,n,i+1,pk(5) s1+, *s2, s1=p5、编程题(1)#include <stdio.h>int main() int a=3,b=7,c=2; int *ptra = &a,*ptrb = &b,*ptrc = &c; int temp; printf("a=%d,b=%d,c=%dn",a,b,c); printf("*ptra=%d,*ptrb=%d,*ptrc=%dn",*ptra,*ptrb,*ptrc);