资源描述:
《C语言课后习题答案.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、第4章后练习10.ch4_lx10.c#includevoidmain(){intm=1,n=1,s=0,k=1;while(n<=10){while(m<=n){printf("%d",m);m++;}printf("");n++;m=1;}}Ch4_lx12.c输入一行字符串,统计有多少字母.#includevoidmain(){intm=0;charch;while((ch=getchar())!='')if((ch>='a'&&ch<='z')
2、
3、(ch>='A'&&ch<='Z'))m++;printf("%d",m)
4、;}}Ch4_lx13.c猜字游戏#include#include#include#includevoidmain(){intmagic;intguess;srand((unsigned)time(NULL));magic=rand();printf("Enteryourguess:");scanf("%d",&guess);while(guess!=magic){if(guess>magic)printf("**Toohigh**");elseprintf("**TooLow**");printf
5、("Enteryourguess:");scanf("%d",&guess);if(guess==magic){printf("**Right**");break;}}}11.输入一自然数,输出它的因子。解1.#includeintmain(){intw,i=2;printf("请输入一个整数w:");scanf("%d",&w);printf("%d的因子是:",w);while(w){while(w){if(w%i==0){printf("%d",i);w=w/i;}elsebreak;}i++;}return0;}Ch5_lx14.c求1*3*
6、5*7…*n#includeintmain(){inti,x,s=1;printf("请输入一个奇数:");scanf("%d",&x);for(i=1;i<=x;i+=2)s=s*i;printf("1*3*...*%d=%d",x,s);return0;}设计一个猜字游戏.程序给出一个数,游戏者猜对了,屏幕显示“**Right**”;当猜的数错了,显示”**Wrong**”,并告之猜的数太小并显示”**TooLow**”,否则就显示”**TooHigh**”.例:猜字游戏(一)main(){intmagic=123;intguess;printf
7、(“Enteryourguess:”;scanf(“%d”,&guess);if(guess==magic)printf(“**Right**”);else{printf(“**Wrong**”);if(guess>magic)printf(“**Toohigh**”);elseprintf(“**Toolow**”);}}main()猜字游戏(二){intmagic=123;intguess;printf("Enteryourguess:");scanf("%d",&guess);while(guess!=magic){if(guess==magic)printf(
8、"**Right**");else{printf("**Wrong**");if(guess>magic)printf("**Toohigh**");elseprintf("**TooLow**");}printf("Enteryourguess:");scanf("%d",&guess);}}例:一球从100米高度自由落下,每次落地后都弹回原高度一半落下,求它在第10次落地时,共经过多少米?第10次反弹多高?main(){floatsn=100,hn=sn/2;intn;for(n=2;n<=10;n++){sn=sn+2*hn;hn=hn/2;}print
9、f(“%fm.”,sn);printf(“%fm.”,hn);}例:猴子吃桃子问题.猴子第一天摘下若干桃子,当即吃了一半,又多吃了一个.第二天早上又将剩余的桃子吃掉一半,还是又多吃了一个.以后每天早上都吃了前天剩下的一半多一个.到第十天早上想再吃时,只见剩下了一个桃子了.求第一天共摘了多少桃子?main(){intday,x1,x2;day=9;x2=1;while(day>0){x1=(x2+1)*2;x2=x1;day--;}printf(“total=%d”,x1);}12.#incl