C语言课后习题答案.doc
第4章后练习10. ch4_lx10.c #include <stdio.h>void main() int m=1,n=1,s=0,k=1; while(n<=10) while(m<=n) printf("%d",m); m+; printf("n"); n+; m=1; Ch4_lx12.c 输入一行字符串,统计有多少字母. #include <stdio.h>void main() int m=0;char ch;while( (ch=getchar() )!='n') if(ch>='a'&&ch<='z')|(ch>='A'&&ch<='Z') m+; printf("%dn",m);Ch4_lx13.c 猜字游戏#include<stdio.h>#include<stdlib.h>#include<math.h>#include<time.h>void main( ) int magic ; int guess; srand( (unsigned) time(NULL) ); magic=rand(); printf("Enter your guess:");scanf("%d",&guess); while( guess!=magic) if(guess>magic) printf(" * Too highn*"); else printf(" * Too Lown*"); printf("Enter your guess:"); scanf("%d",&guess); if(guess =magic) printf("*Right*n"); break; 11. 输入一自然数,输出它的因子。解1. #include<stdio.h>int main()int w,i=2; printf("请输入一个整数w: n"); scanf("%d",&w); printf("%d的因子是:",w);while(w) while(w) if(w%i=0) printf(" %d ",i); w=w/i; else break; i+; return 0; Ch5_lx14.c 求 1*3*5*7*n#include<stdio.h>int main()int i,x,s=1;printf("请输入一个奇数:n");scanf("%d",&x);for(i=1;i<=x;i+=2)s=s*i;printf("1*3*.*%d=%dn",x,s);return 0;设计一个猜字游戏. 程序给出一个数,游戏者猜对了,屏幕显示 “*Right*”; 当猜的数错了,显示”*Wrong*”,并告之猜的 数太小并显示” * Too Low*”,否则就显示 ” *Too High*”.例:猜字游戏(一) main( ) int magic =123; int guess; printf(“Enter your guess:”; scanf(“%d”,&guess); if(guess =magic) printf(“*Right*”); else printf(“ * Wrong*”); if(guess>magic) printf(“ * Too highn*”); else printf(“ * Too low *n”); main( ) 猜字游戏(二) int magic =123; int guess; printf("Enter your guess:"); scanf("%d",&guess); while(guess!=magic) if(guess =magic) printf("*Right*n"); else printf(" * Wrong*n"); if(guess>magic) printf(" * Too highn*"); else printf(" * Too Lown*"); printf("Enter your guess:"); scanf("%d",&guess); 例:一球从100米高度自由落下,每次落地后都弹回原高度一半落下,求它在第10次落地时, 共经过多少米?第10次反弹多高?main() float sn=100,hn=sn/2; int n; for(n=2;n<=10;n+) sn=sn+2*hn; hn=hn/2; printf(“%f m.n”,sn); printf(“%f m.n”,hn); 例: 猴子吃桃子问题. 猴子第一天摘下若干桃子,当即吃了一半,又多吃了一个. 第二天早上又将剩余的桃子吃掉一半,还是又多吃了一个. 以后每天早上都吃了前天剩下的一半多一个. 到第十天 早上想再吃时,只见剩下了一个桃子了. 求第一天共摘了多少桃子?main()int day,x1,x2; day=9; x2=1; while(day>0) x1=(x2+1)*2; x2=x1; day-; printf(“total=%dn”,x1); 12. #include <stdio.h>void main() int m=0;char ch;while( (ch=getchar() )!='n') if(ch>='a'&&ch<='z')|(ch>='A'&&ch<='Z') m+; printf("%dn",m);第5章后练习把100200之间的不能被3整除的数输出。 main( ) int n; for(n=100; n<=200; n+) if(n%3 =0) continue; printf(“%5d” , n); 例: 问程序输出的结果? main() int i=1,a=0; for(; i<=5; i+) do i+; a+; while(i<3); i+; printf(“a=%d,i=%dn”,a,i); 输出:a=3 ,i=8例: 找出所有的三位数,它和它的各位数字的立方和正好相 等. 例如:153=13+53+33. 设所求的三位数,其百位数字是i,十位数字是j,个位数字 是k. 显然应满足如下表达式: i*i*i+j*j*j +k*k*k = 100*i+10*j+kmain( ) int i,j,k; for(i=1; i<=9; i+) for(j=0; j<=9; j+) for(k=0; k<=9; k+) if(i*i*i+j*j*j +k*k*k = =100*i+10*j+k ) printf(“%d%d%dn”, i,j, k); 第6章后练习示例: 输入2个数,如果它们的值不相等,则交换并输出它们的值;否则,输出“equal”。 交换a和b值的方法: temp=a; a=b; b=temp;#include <stdio.h>void main( ) int a, b, temp; printf("input a, b: "); scanf("%d%d", &a, &b); if(a != b) temp = a; a = b; b = temp; printf("a = %d, b = %dn", a, b); else printf("equaln");示例: 输入3个整数,输出其中的最大值#include <stdio.h>void main( ) int a, b, c, max; printf("input a, b, c: "); scanf("%d%d%d", &a, &b, &c); max = a; if(max < b) max = b; if(max < c) max = c; printf("max is %dn", max);例: 输入x,计算下列分段函数f(x). 1/x x0 f(x)= 0 x=0#include <stdio.h>void main()float x,y;printf("input x: n");scanf("%f",&x);if ( x != 0 ) y = 1/x;else y = 0;printf("f(%f) = %.2fn", x, y);例: 输入x,计算下列分段函数f(x). x+1 x <1 f(x)= x+2 1x<2 x+3 x 2#include<stdio.h>void main( ) double x,y; printf("请输入x:"); scanf("%lf",&x); if(x<1) y=x+1; else if(x<2) y=x+2;else y=x+3; printf("f(%.2f)=%.2fn",x,y); 7. 将大写字符转换为小写. #include<stdio.h>void main() char ch; while(ch=getchar()!='n') if(ch>='A'&&ch<='Z') ch+=32; printf("%c",ch); else printf("%c",ch); printf("n");8. 统计字符#include <stdio.h>#include <math.h>int main() char ch; int x=0,y=0,z=0,a=0,b=0; printf("请输入:n");while(ch=getchar()!='n') if(ch>='a'&&ch<='z') x+; else if(ch>='A'&&ch<='Z') y+; else if(ch>='0'&&ch<='9') z+; else if(ch=' '|ch='n') a+; else b+; printf("小写字母%d个n",x); printf("大写字母%d个n",y); printf("数字字符%d个n",z); printf("空格字符%d个n",a); printf("其它字符%d个n",b); return 0;9计算分段函数#include<stdio.h>void main() float x,y; printf("请输入x:n"); scanf("%f",&x); if(x<=1) y=x+5; else if( x>1&&x<=10 ) y=2*x; else y=(3/(x-10);printf("x=%f y=%fn",x,y);10.五分制与百分制的转换#include<stdio.h>void main() int x,i; printf("该程序功能为五分制与百分制的转换!n"); printf("1 进入, 2 退出!n"); scanf("%d",&i);while(i=1) printf("请输入:n"); scanf("%d",&x); if(x>=90 && x<=100) x=5; else if(x>=80 && x<90) x=4; else if(x>=70&&x<80) x=3; else if(x>=60&&x<70) x=2; else x=1;printf("五分制成绩为%d分n",x); printf(“1 继续 2 退出!n"); scanf("%d",&i); 第7章后练习1百分制转换成五分制. #include<stdio.h> int main() int x;printf("请输入学生百分制成绩:n输入负数结束程序:n");scanf("%d",&x);while(x>0) switch(x/10) case 10: case 9: printf("五分制为:n5nn");break; case 8: printf("五分制为:n4nn");break;case 7: printf("五分制为:n3nn");break;case 6: printf("五分制为:n2nn");break; default: printf("五分制为:n1nn");break; printf("请输入学生百分制成绩:n输入负数结束程序:n"); scanf("%d",&x); printf("谢谢使用,再见:n");return 0;2计算sin(),cos(),tan().#include <stdio.h>#include<math.h>int main()double x,s; int y;printf("Input a number:n");scanf("%lf",&x);while(1) printf("您希望进行哪种运算?n"); printf("1.计算sin(x)n"); printf("2.计算cos(x)n"); printf("3.计算tan(x)n"); scanf("%d",&y); switch(y) case 1: s=sin(x); printf("sin(%f)=%fn",x,s);break; case 2: s=cos(x); printf("cos(%f)=%fn",x,s);break; case 3: s=tan(x); printf("tan(%f)=%fn",x,s);break; default: printf(“输入有误,请输入 1 - 3之间的数字!n"); If(y=1|y=2|y=3) break; printf("谢谢使用:n"); return 0;Acknowledgements My deepest gratitude goes first and foremost to Professor aaa , my supervisor, for her constant encouragement and guidance. She has walked me through all the stages of the writing of this thesis. Without her consistent and illuminating instruction, this thesis could not havereached its present form. Second, I would like to express my heartfelt gratitude to Professor aaa, who led me into the world of translation. I am also greatly indebted to the professors and teachers at the Department of English: Professor dddd, Professor ssss, who have instructed and helped me a lot in the past two years. Last my thanks would go to my beloved family for their loving considerations and great confidence in me all through these years. I also owe my sincere gratitude to my friends and my fellow classmates who gave me their help and time in listening to me and helping me work out my problems during the difficult course of the thesis. My deepest gratitude goes first and foremost to Professor aaa , my supervisor, for her constant encouragement and guidance. She has walked me through all the stages of the writing of this thesis. Without her consistent and illuminating instruction, this thesis could not havereached its present form. Second, I would like to express my heartfelt gratitude to Professor aaa, who led me into the world of translation. I am also greatly indebted to the professors and teachers at the Department of English: Professor dddd, Professor ssss, who have instructed and helped me a lot in the past two years. Last my thanks would go to my beloved family for their loving considerations and great confidence in me all through these years. I also owe my sincere gratitude to my friends and my fellow classmates who gave me their help and time in listening to me and helping me work out my problems during the difficult course of the thesis.