C语言课后习题答案.doc

上传人:laozhun 文档编号:2384756 上传时间:2023-02-17 格式:DOC 页数:10 大小:44KB
返回 下载 相关 举报
C语言课后习题答案.doc_第1页
第1页 / 共10页
C语言课后习题答案.doc_第2页
第2页 / 共10页
C语言课后习题答案.doc_第3页
第3页 / 共10页
C语言课后习题答案.doc_第4页
第4页 / 共10页
C语言课后习题答案.doc_第5页
第5页 / 共10页
点击查看更多>>
资源描述

《C语言课后习题答案.doc》由会员分享,可在线阅读,更多相关《C语言课后习题答案.doc(10页珍藏版)》请在三一办公上搜索。

1、第4章后练习10. ch4_lx10.c #include 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 void main() int m=0;char ch;while( (ch=getchar() )!=n) if(ch=a&ch=A&ch=Z) m+; printf(%dn,m);Ch4_lx13.c 猜字游戏#include#include#include#include

2、void main( ) int magic ; int guess; srand( (unsigned) time(NULL) ); magic=rand(); printf(Enter your guess:);scanf(%d,&guess); while( guess!=magic) if(guessmagic) printf( * Too highn*); else printf( * Too Lown*); printf(Enter your guess:); scanf(%d,&guess); if(guess =magic) printf(*Right*n); break; 1

3、1. 输入一自然数,输出它的因子。解1. #includeint 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#includeint main()int i,x,s=1;printf(请输入一个奇数:n);scanf(%d,&x);for(i=1;imagic) printf(“ * Too highn*

4、”); 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(guessmagic) printf( * Too highn*); else printf( * Too Lown*); printf(Enter your guess:); scanf(

5、%d,&guess); 例:一球从100米高度自由落下,每次落地后都弹回原高度一半落下,求它在第10次落地时, 共经过多少米?第10次反弹多高?main() float sn=100,hn=sn/2; int n; for(n=2;n0) x1=(x2+1)*2; x2=x1; day-; printf(“total=%dn”,x1); 12. #include void main() int m=0;char ch;while( (ch=getchar() )!=n) if(ch=a&ch=A&ch=Z) m+; printf(%dn,m);第5章后练习把100200之间的不能被3整除的数输

6、出。 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(i3); 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*

7、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 void main( ) int a, b, temp; printf(input a, b: ); sc

8、anf(%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 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,计算下

9、列分段函数f(x). 1/x x0 f(x)= 0 x=0#include 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 1x2 x+3 x 2#includevoid main( ) double x,y; printf(请输入x:); scanf(%lf,&x); if(x1) y=x+1; else if(x2) y=x+2;els

10、e y=x+3; printf(f(%.2f)=%.2fn,x,y); 7. 将大写字符转换为小写. #includevoid 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 #include 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=A&ch=0&ch=9)

11、 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计算分段函数#includevoid main() float x,y; printf(请输入x:n); scanf(%f,&x); if(x1&x=10 ) y=2*x; else y=(3/(x-10);printf(x=%f y=%fn,x,y);10.五分制与百分制的转换#includevoid

12、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=80 & x=70&x=60&x70) x=2; else x=1;printf(五分制成绩为%d分n,x); printf(“1 继续 2 退出!n); scanf(%d,&i); 第7章后练习1百分制转换成五分制. #include int main() int x;printf(请输入学生百分制成绩:n输入负数结束程序:n);sc

13、anf(%d,&x);while(x0) 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()

14、.#include #includeint 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;

15、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 thr

16、ough 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

17、 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 sin

18、cere 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 gu

19、idance. 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.

20、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.

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 建筑/施工/环境 > 项目建议


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号