计算机c语言习题答案.pdf
《计算机c语言习题答案.pdf》由会员分享,可在线阅读,更多相关《计算机c语言习题答案.pdf(71页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、C语言习题答案第1章1 .选择题(1)c(2)B (3)D2.填空题(l)main(2)main(3)有穷性、确定性、有零个或多个输入、有一个或多个输出、有效性(4)顺序、分支和循环(5)自顶向下,逐步细化、模块化设计、结构化编码第2章1 .选择题B B C DA DDB B A B B DC B2、填空题(1)数字、字母、下划线(2)0 (3)4、8 (4)(a+b)*c/(a1 b)(5)6 0 (6)1 6(7)3 (8)6、4、2(9)0 (1 0)1 0、6 (1 1)5.5 0 0 0 0 0 (1 2)1 2、4(1 3)double(1 4)0 (1 5)1 6 (1 6)6.
2、63、编程题(1)main()p r int f(int:%5 dnA float :%5 dn/z“char:%5 dn“double:%5 dn/zlong:%5 dn,z,s izeof(int),s izeof(float),s izeof(char),s izeof(double),s izeof(long);(2)t t include t t define RA T 1.6 0 9 3 4main()float k=0.0;p r int f(inp ut t he km:);s canf(f,&k);p r int f(z/nmile:%f/z,k*RA T);第3章1 .选择
3、题(1)(1 0):DDC DC DC DB C2.解析题(1)x=1 7 0,x=L-I MU 1 7 0,x=L_|ML.|25 2,1 7 0X=1 7 0,X=1 7 0 L-l U M ,X=L-J L-J L-l1 7 0,x=%6 da=5 1 3.7 8 9 1 8 5,a=M M5 1 3.7 9,a=5 1 3.7 8 9 1 8 4 5 7,a=5 1 3.7 8 9 1 8 4 5 7(2)a=3 U b=7 x=8.5 U y=7 1.8 2cl=A Mc2=a3.编程题(1)main()(int x,y;s canf(%d%d,&x,&y);p r int f(商数
4、二%d,余数二 d,x/y,x%y);s ys t em(p aus e);(2)main()double x,y,z,avg;s canf(/z%lf%lf%lfz/,&x,&y,&z);avg=(x+y+z)/3;p r int f(.llf/z,avg,avg);s ys t em(p aus e);第4章1.选择题(1)(1 0)C C A A DC C A B D2.填空题(1)ch=,A,&ch二 Zch=ch 3 2x-ly=-l(2)x 2(3)a+b ca=b&a=c&a+c b&b+c aa=ba=cb=c(4)mar k/1 0k=lcas e 9(5)x 0c=x/1
5、0y!=-23.编程题(1)t t include main()(int x;p r int f(,zp leas e inp ut anumber:);s canf(%d,&x);if(x%2=0)p r int f(zzx is a evennumber );els e p r int f(x is a oddnumber );(2)t t include main()(int x,y;p r int f(/zp leas e inp ut anumber );s canf(d,&x);if(x=-5)p r int f(zzt he number iser r or );els e if
6、(x 0)p r int f(%d,y);)els ey=x-l;p r int f(%d,y);els e尸x;if(x=0)if(x 1 0)y=x+l;p r int f(级d,y);els e p r int f(t he number iser r or );(3)#include main()int a,m;p r int f(/zp leas e inp ut a number:);s canf(%d,&a);s wit ch(a/1 0)cas e 0:cas e 1:cas e 2:m=l;br eak;cas e 3:m=2;br eak;cas e 4:m=3;br ea
7、k;cas e 5:m=4;br eak;default:m=5;br eak;)p r int f(%d,m);)(4)t t include main()float p r ice,t ax;p r int f(/zp leas e inp ut t he p r ice ofp r oduct:);s canf(%f,fep r ice);if(p r ice 1 0 0 0 0)t ax=p r ice*。.0 5;els e if(p r ice 5 0 0 0)t ax=p r ice*。.0 3;els e if(p r ice 1 0 0 0)t ax=p r ice*。.0
8、2;els e t ax=0;p r int f(%f,t ax);(5)t t include main()float s cor e;p r int f(/zp leas e inp ut t he s cor e ofs t udent:);s canf(f,&s cor e);if(s cor e=8 5)p r int f(VERY G OOD);els e if(s cor e=6 0)p r int f(G OOD);els e p r int f(B A D);)第5章1 .选择题(l)d(2)c(3)a(4)d(5)a(6)d(7)d(8)b(9)d(1 0)b(ll)c(1
9、 2)b(1 3)d(1 4)a(1 5)c2.填空题(1)=0 m=m/k k+(2)5 4 6(3)3*i-2(4)-=*二 (5)8 5 2(6)j+i%j=0 j=i(7)s um k s um=二k j-2(8)s=0P=1 j=i3.改错题(1)第一处改正:F or改为for第 二 处 改 正:ave=s um/4改为ave=s um/4.0(2)第一处改正:j=9第二处改正:m=1 0 0*i+1 0*j+k3.编程题(1)t t include main()(int s;float n,t,s um;t=l;s um=0;n=l;s=l.0;while(n=1 0 0)s um
10、=s um+t;n=n+l;s=s;t=s/n;p r int f(/zs um=%1 0.6 fn,s um);)(2)利用辗除法,直到b为。为止main()int p,r,n,m,t emp;p r int f(inp ut t wo int eger n,m:);s canf(/z%d,%d,&n,&m);if(n 0)xl=(x2+l)*2;/*第一天的桃子数是第2天桃子数加1后的2倍*/x2=xl;day-;p r int f(/zt he t ot al is%dn,xl);)(4)t t include main()int i;long int n=l;for (i=l;i=1
11、0;i+)(n=n*i;p r int f(i%5=0?z/%2d!=%T0 1 dn :级2d!=%T0 1 d,i,n);)(5)main()(int m,s,i;for(m=2;m 1 0 0 0;m+)s=0;for(i=l;i m;i+)if(m%i)=0)s=s+i;if(s=m)p r int f(/z%d it s fact or s ar e ,m);for(i=l;i m;i+)if(m%i=0)p r int f(线d,,i);p r int f(n);#include main()(int n,i,j;scanf(%d,&n);for(i=l;i=n;i+)for(j=
12、l;j=n+i-l;j+)if(j=n-i)printf(/z );else printf(*);printf(n);)(7)#includemain()(float xO,xl,x2,fxO,fxl,fx2;do p r int f(/zEnt er xl&x2:);s canf(/z%f,%f,&xl,&x2);fxl=xl*(2*xl1 4)*xl+3)-6;fx2=x2*(2*x2-4)*x2+3)-6;while(fxl*fx2 0);do x0=(xl+x2)/2;fxO=xO*(2*x0-4)*x0+3)-6;if(fxO fxl)=le-5);p r int f(x=%6.2f
13、n,xO);)第 6章1.选择题C C DC C A A DB D2.写出程序的运行结果(1)s um=6 (2)Max=9,r ow=l,col=0(3)01 02 1 03 2 10(4)-53、填空(1)i=j=k=0 、i 4 、j(2)s i、s 0,s 1 s t r,s 1 s 2,s t r4、编程题(1)#include main()(int a1 2,count=0,i;r andomize();for (i=0;i 1 2;i+)ai=5+r andom(1 1);for(i=0;i12;i+)(printf(%5d,a i);count+;if(count%4=0)pr
14、intf(n)getchO;(2)main()(int a5=2,23,43,77,33);int max,min,i;long sum=OL;int count=0;float av;max=min=a0;for(i=0;imax)max=ai;if(ai min)min=ai;)av=(float)sum/5;for(i=0;iav)count+;printf(max:%dnmin:%dnav:%fncount:%d,max,min,av,count);getchO ;)(3)main()(intscore 30 =61,62,63,64,65,66,67,68,69,70,71,72,7
15、3,74,75,76,77,78,79,80,81,85,89,90,33,44,55,91,92,93);inti,count 9 0=0,count 8 0=0,count 7 0=0,count6 0=0;for (i=0;i 3 0;i+)(p r int f(/zint p ut NO.%d s t udent ss cor e:,i+1);s canf(/z%d/z,s cor e+i);)for (i=0;i=9 0)(count 9 0+;count 8 0+;count 7 0+;count 6 0+;)els e if(s cor ei=8 0)(count 8 0+;co
16、unt 7 0+;count 6 0+;else if(scorei=70)count70+;count60+;)else if(scorei=60)(count60+;printf(0 60:%dn60 70:%dn70 80:%dn80 90:%dn90 10 0:%d,30-count60,count60-count70,count70-count80,count80-count90,count90);printf(z/n60 or higher:%dn70 orhigher:%dn80 or higher:%dn90 orhigher:%dnz/,count60,count70,cou
17、nt80,count90);getch();(4)1、main()char a 4 5;int i,j,k;for (i=0;i 4 5;i+)ai二 for(i=0;i 5;i+)for(j=0;j i;j+)a9*i+j=;for(k=0;k 5;k+)a9*i+j+k=*;)j=0;for(i=0;i(x y?x:y)(9)s t r =abdef(1 0)8,1 7(1 1)a=5a*a=25a*a*a=5 1 2(1 2)si*XTX XT*XP XT XT*#xix viz*Jx 1*TX TX XTX XTX XT*XTXfx 1*1*TX XIX XT*XTX#1*TX TX
18、XT*TXxix viz*Jx 1*TX TX XTX XTX XT*XTX#si*1 1*XT*TX XTX T Xj3、编程题(1)long act (long n)if(n=l|n=0)r et ur n I L;els ereturn n*act(nT);main()(int n=3;p rin tf(n!=%ld,act(n);getchO;)(2)void mmax(float a,int n);main()float a6;int i;fo r(i=l;i)flagul;i+;+while(flag&i=A&(a)=a&(a)=zD)main()char c;s canf(c,&
19、c);p r int f(z/%dn/z,I SA LPH A(c);)(2)/*习题 8-2-2*/#define SWA P(t,x,y)t w;w=x;x=y;y=w;main()double a=9 9.9 9,b=ll.1 1;SWA P(double,a,b);p r int f(%f,%fn/z,a,b);(3)/*习题 8-2-3 */Sinclude s t dio.ht t define MA X 8 0t t define C H A NG E 1main()char s t r MA X;int i;p r int f(I np ut t ext:n/z);get s(
20、s t r);#if(C H A NG E)for (i=0;i=,a,&s t r izs t r i=,A&s t r i,Z,)s t r i+=l;els eif(s t r i=二z|s t r i=,Z)s t r i-=25;)t t endifp r int f(out p ut:n%s,s t r);)(4)/*习题 8-2-4 */t t define LEA P_Y EA R(y)(y%4=0)&(y%1 0 0!=0)|(y%4 0 0=0)main()int year;p r int f(Z/I np ut year:/z);s canf(d,&year);if(LE
21、A P_Y EA R(year)p r int f(/z%d is a leapyear.n,year);els ep r int f(/z%d is not a leapyear.n,year);)(5)/*习题 8-2-5.h*/t t ifndef X8 _2_5 _Ht t define X8 _2_5 _H#def ine A VER(a,b,c)(a+b+c)/3.0Sendif/*习题 8-2-5 */#include X8 _2_5.h main()int a,b,c;p r int f(I np ut t hr ee int eger sa,b,c:);s canf(%d,%
22、d,%d,&a,&b,&c);p r int f(z/A ver ageis%f.n/z,A VER(a,b,c);第 9 章4.编程题(1)/*习题 9-4-1*/#include ttinclude main()char*strl20,*str220,*str3 20;char swap();printf(Input three line:nz/);gets(strl);gets(str2);gets(str3);if(strcmp(strl,str2)0)swap(strl,str2);if(strcmp(strl,str3)0)swap(strl,str3);if(strcmp(str
23、2,str3)0)swap(str2,str3);printf(Now,the order is:n);printf(/z%sn%sn%sn/z,strl,str2,str3)char swap(char*pl,char*p2)/*交换两个字符串*/char*p20;strcpy(p,pl);strcpy(pl,p2);strcpy(p2,p);(2)/*习题 9-4-2*/main()int number20,n,m,i;printf(Z/How many numbers?);/*共有多少个数*/scanf(%d,&n);printf(Input%d numbers:n,n);/*输入n 个
24、数*/for(i=0;in;i+)scanf(/z%d/z,&number i);printf(/zHow many place you want tomove?);/*后移多少个位置*/s canf(%d,&m);move(number,n,m);/*调用move函数*/p r int f(/zNow,t hey ar e:nz/);for (i=0;i ar r ay;p-)*p=*(p-l);*ar r ay=ar r ay_end;m;if(m 0)move(ar r ay,n,m);/*递归调用,当循环次数in减至0时,停止调用*/(3)/*习题 9-4-3*/#include#in
25、cludettdefine TO TAL 7int mseek(char*str,char xstr,int n)int i;for(i=0;in;i+)if(strcmp(stri,xstr)=0)return 1;return 0;)main()char*nameTO TAL=Lining,Linshan,1lanyuan,Zhangqiang,Haipo,rangbing;char xname20;printf(enter a name:);gets(xname);if(mseek(name,xname,TO TAL)printf(Found!n);els ep r int f(Not
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 计算机 语言 习题 答案
限制150内