《2021年C语言程序设计形成性考核册参考答案.pdf》由会员分享,可在线阅读,更多相关《2021年C语言程序设计形成性考核册参考答案.pdf(37页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、一、选取题1.在每个C语言程序中都必要包具有这样一种函数,该函数函数名为(A)。A.m a i n B.M A I N C.na m e D.f u nt i o n2.C语言原程序文献缺省扩展名为(A)。A.c p p B.e x e C.o b j D.C3 .由C语言目的文献连接而成可执行缺省扩展名为(B)。A.c p p B.e x e C.o b j D.C4 .程序运营中需要从键盘输入多于一种数据时,各数据之间应使用(D)符号作为分隔符。A.空格或逗号 B.逗号或回车 C.回车或分号 D.空格或回车5 .每个C语言程序编译错误分为(B)类。A.1 B.2 C.3 D.46 .设 x
2、和 y 均为逻辑值,则 x&y为真条件是(A),A.它们均为真 B.其中一种为真 C.它们均为假 D.其中一种为假7 .设有语句“i nt a=12;a+=a*a;,则执行结束后,a值 为(C)。A.12 B.14 4 C.15 6 D.28 88 .x 0&x=10相反表达式为(A).A.x 10 B.x 10 C.x=0 I I x 0&x 109 .字符串“a+b=12 n”长 度 为(B)A.6 B.7 C.8 D.910.在下列符号常量定义中。错误定义语句格式为(C)oA.c o ns t M l=10;B.c o ns t i nt M 2=20;C.c o ns t M 3 10
3、 D.c o ns t c ha r m a r k=(3;11.带有随机函数表达式r a nd()%20值 在(C)区间内,A.r i 9 B.20 C.0 19 D.0 2012.当解决特定问题时循环次数已知时,普通采用(A)循环来解决。A.f o r B.whi l e C.d o-whi l eD.s wi t c h13 .在s wi t c h语句每个c a s e块中,假定都是以b r e a k语句结束,则此s wi t c h语句容易被改 写 为(B)语句。A.f o r B.i f C.d o D.whi l e14 .f o r语句可以被改写为(D)语句。A.复合 B.i
4、 f C.s wi t c h D.whi l e15 .下面循环语句执行结束后输出i值 为(B)。f o r(i nt i=0;i n/2)c o u t i e nd l;b r e a k;A.n/2 B.n/2+1 C.n/2-1 D.n-116 .在下面循环w语句中内层循环体S语句执行次数为(D)of o r (i nt i=0;i n;i+)f o r(i nt j=i;j n;j+)S;A.n2 B.(n+l)/2 C.n(n-l)/2 D.n(n+l)/217 .在下面d o循环语句中,其循环体被执行次数为(A )。i nt i=0;d o i+;whi l e(i*i 5 相
5、反表达式为x+y b|b=5 相反表达式为 a =b&(b 5|b y 逻辑值为 f a l s e3 5 .若 x=5,y=10,则 x=y 逻辑值为一 t r u e 一3 6 .假定x=5,则执行“a=(x?10:2 0);”语句后a 值为_ 10_。3 7 .执 行“t y pe d e f i n t Da t a T y pe;”语句后,在 使 用 i n t 定义整型变量地方都可以使用 Da t a T y pe _来定义整型变量。3 8 .在 s w i t c h 语句中,每个语句标号所含保存字c a s e 背面表达式必要是整型一。3 9 .作为语句标号使用C 保存字c a
6、 s e 和 d e f a u l t 只能用于_ s w i t c h _ 语句定义体中。4 0.在 s w i t c h 语句时,在进行作为条件表达式求值后,将从某个匹配标号位置起向下执行,当遇到下一种标号位置时(停止/不断止)不断止执行。4 1.若 d o 循 环“尾”为“w h i l e(+i b)_ _ _7 .与构导致员访问表达式(*(力)松。代等价表达式是fp-score。三、写出下列每个程序运营后输出成果1.#i nc l u d e v o i d m a i nO(i nt x=5;s w i t c h(2*x-3)c a s e 4:p r i nt f (z
7、z%d ,x);c a s e 7:p r i nt f C%d ”,2*x+l);c a s e 1 0:p r i nt f (z,%d ,3*x-l);b r e a k;d e f a u l t:p r i nt f (z,%s ,d e f a u l t n);)p r i nt f(%s n,s w i t c h e nd.);输出成果为:1 1 1 4 s w i t c h e nd.P r e s s a ny k e y t o c o nt i nu e2.#i nc l u d e v o i d m a i n()Ii nt i,s=0;f o r(i=l;i
8、=6;i+)s+=i*i;p r i nt f(s=/d n,s);)输出成果为:s=9 1.P r e s s a ny k e y t o c o nt i nu e3.#i nc l u d e v o i d m a i n()Ii nt i,s l=0,s 2=0;f o r (i=0;i 1 0;i+)i f(i%2)s l+=i;e l s e s 2+=i;p r i nt f (z,%d%d n”,s i,s 2);)输出成果为:2 5 2 0.P r e s s a ny k e y t o c o nt i nu e4.#i nc l u d e v o i d m a
9、i n()Ii nt n=1 0,y=l;w h i l e(n-)y+;y+;p r i nt f(z,y=%d n,z,y);)输出成果为:y=2 1.P r e s s a ny k e y t o c o nt i nu e5.#i nc l u d e v o i d m a i n()(i nt f,f l,f 2,i;f l=f 2=l;p r i nt f (z,%d%d z/,f l,f 2);f o r(i=3;i =1 0;i+)f=f l+f 2;p r i nt f(d ,f);i f(i%5=0)p r i nt f C n,z);f l=f 2;f 2=f;)p
10、r i nt f (n);)输出成果为:112 3 58 1 3 2 1 3 4 5 5P r e s s a ny k e y t o c o nt i nu e6.#i nc l u d e#i nc l u d e v o i d m a i n()(i nt i,n;f o r (n=2;n=2 0;n+)i nt t e m p=(i nt)s q r t (n);s q r t (n)求出 n 平方根并取整f o r(i=2;it e m p)pr in t f (z,%d ”,n);)pr in t f (n);输出成果为:2 3 5 7 11 13 17 19Pr e s s
11、an y k e y t o co n t in u e7.#in cl u d e#in cl u d e co n s t in t M=20;v o id m ain ()(in t i,c2,c3,c5;c2=c3=c5=0;f o r (i=l;i=M;i+)if(i%2=0)c2+;if(i%3=0)c3+;if(i%5=0)c5+;)pr in t f C%d%d%d n ,c2,c3,c5);输出成果为:10 6 4Pr e s s an y k e y t o co n t in u e8.#in cl u d e#in cl u d e co n s t in t M=20
12、;v o id m ain()(in t i,s;f o r(i=l,s=0;i15;i+)if (i%2=0|i%3=0)co n t in u e;pr in t f (z/%d ”,i);s+=i;)pr in t f(%d n,s);)输出成果为:1 5 7 11 13 37Pr e s s an y k e y t o co n t in u e1.#in cl u d e v o id m ain()in t a 10=12,39,26,41,55,63,72,40,83,95;in t i,il=0,i2=0;f o r(i=0;i10;i+)if(a i%2=l)il+;e l
13、 s e i2+;pr in t f (,z%d%d n”,il,i2);输出成果为:6 4Pr e s s an y k e y t o co n t in u e2.#in cl u d e#in cl u d e v o id m ain()Iin t i;char *a 5=s t u d e n t,w o r k e r”,cad r e,“s o l d ie r,pe as an t;char *pl,*p2;pl=p2=a 0;f o r(i=0;i0)pl=a i;if(s t r cm p(a i,p2)0)p2=a i;)pr in t f (z,%s%s n/z,pl
14、,p2);)输出成果为:w o r k e r cad r ePr e s s an y k e y t o co n t in u e3.#in cl u d e in t a 10=4,5,6,15,20,13,12,7,8,9);v o id m ain ()in t i,s O,s i,s 2;s 0=s l=s 2=0;f o r(i=O;i10;i+)s w it ch(a i%3)cas e 0:s 0+=a i;br e ak;cas e 1:s l+=a i;br e ak;cas e 2:s 2+=a i;br e ak;i)pr in t f (z,%d%d%d n ,s
15、 O,s i,s 2);)输出成果为:42 24 33Pr e s s an y k e y t o co n t in u e4.#in cl u d e v o id m ain O!char a 口 二 abcd bf bg acd”;in t il=0,i2=0,i=0;w hil e(a i)if (a i=,a)il+;if (a i=,b)i2+;i+;)pr in t f (,z%d%d%d n,il,i2,i);)输出成果为:2 3 11Pr e s s an y k e y t o co n t in u e5.8in cl u d e v o id m ain()Iin
16、t a 3 4=1,2,7,8,5,6,10,6,9,12,3,4);in t m=a 0 0;in t ii=0,j j=O;in t i,j;f o r (i=0;i3;i+)f o r(j=0;j 4;j+)if(a i m=a i j;ii=i;j j=j;pr in t f C%d%d%d nz/,ii,j j,a ii j j);)输出成果为:2 1 12Pr e s s an y k e y t o co n t in u e6.#in cl u d e v o id m ain()in t a,b;f o r(a=l,b=2;b50;)pr in t f (,z%d%d ”,a
17、,b);a=a+b;b=a+b;)pr in t f(n);)输出成果为:1 2 3 5 8 13 21 34Pr e s s an y k e y t o co n t in u e三、写出下列每个程序运营后输出成果1.#in cl u d e v o id S B(char ch)s w it ch(ch)cas e A :cas e a,:pr in t f(W W);br e ak;cas e B:cas e b,:pr in t f(GG);br e ak;cas e C :cas e c:pr in t f(PP);br e ak;d e f au l t:pr in t f(BB
18、);br e ak;)v o id m ain()char al=b,a2=,C,,a3=f ;S B(al);S B(a2);S B(a3);S B(A);p r i n t f C W);)输出成果为:GGPPBBW WPr e s s an y k e y t o co n t in u e2.#in cl u d e#in cl u d e d o u bl e S D(in t a,in t b,char o p)d o u bl e x;s w it ch(o p)cas e +:x=a+b;br e ak;cas e -:x=a-b;br e ak;cas e :x=a*b;br
19、 e ak;cas e y/y:if (b)x=(d o u bl e)a/b;e l s e e x it(1);br e ak;d e f au l t:(运算符错!n);e x it(1);)r e t u r n x;)v o id m ain()in t x=20,y=8;pr in t f (%3.21f ”,S D(x,y,;pr in t f (,z%3.21f ,S D(x,y,*);pr in t f(%3.21f ,S D(x,y,/);)输出成果为:12.00 160.00 2.50Pr e s s an y k e y t o co n t in u e3.#in c
20、l u d e v o id W F(in t x,in t y)x=x+y;y=x+y;pr in t f (s u bs:x,y=%d,%d nz,,x,y);v o id m ain()in t x=18,y=23;pr in t f(m ain:x,y=%d,%d n ,x,y);W F(x,y);x=2*x;pr in t f (m ain:x,y=%d,%d n/z,x,y);)输出成果为:m ain:x,y=18,23s u bs:x,y=41,64m ain:x,y=36,23Pr e s s an y k e y t o co n t in u e4.#in cl u d e
21、#in cl u d e v o id f u n(char s s );v o id m ain()charf u n(s);pr in t f(%s n,s);)v o id f u n(char s s )in t i,n=s t r l e n(s s);f o r (i=0;in/2;i+)char c=s s i;s s i=s s n-l-i;s s n-l-i=c;)输出成果为:Pr e s s an y k e y t o co n t in u e5.#in cl u d e v o id In s e r t S o r t(in t a,in t n)(in t i,j
22、,x;f o r (i=l;i n;i+)进行 n 次循环x=a i;f o r(j=i-l;j=0;j)为 x 顺序向前寻找适当插入位置if (x a j)a j+l=a j;e l s e br e ak;a j+l=x;)v o id m ain()Iin t i;in t a 6=20,15,32,47,36,28;In s e r t S o r t(a,6);f o r (i=0;i6;i+)pr in t f(%d ,a i);pr in t f(n);)输出成果为:47 36 32 28 20 15Pr e s s an y k e y t o co n t in u e6.#
23、in cl u d e v o id m ain()in t a 8=3,5,7,9,11,13,15,17);in t i,*p=a;f o r(i=0;i8;i+);pr in t f C%5d ,*p+);if(i+l)%4=0)pr in t f(n);)输出成果为:3 5 7 911 13 15 17Pr e s s an y k e y t o co n t in u e7.#in cl u d e in t L A(in t *a,in t n)in t i,s=0;f o r(i=0;in;i+)s+=a i;r e t u r n s;v o id m ain ()in t
24、a =5,10,15,20,25,30);in t b=L A(a,4);in t c=L A(a+2,3);pr in t f (,z%d%d n,b,c);)输出成果为:50 60Pr e s s an y k e y t o co n t in u e8.#in cl u d e in t L B(in t *a,in t n)in t i,s=l;f o r(i=0;in;i+)s*=*a+;r e t u r n s;)v o id m ain O in t a =l,2,3,4,2,4,5,2);in t b=L B(a,4)+L B(&a 3,4);pr in t f(b二%d
25、n,b);)输出成果为:b=184Pr e s s an y k e y t o co n t in u e1.#in cl u d e s t r u ct W o r k e r char name 15;姓名int age;年龄float pay;工资);void main()struct Worker x=Mwanghuan,52,23.50);struct Worker y,*p;y=x;p=&x;printf(%s%d%6.2fn,y.name,y.age,y.pay);printf(%s%d%6.2fnn,p-name,p-age,p-pay);)输出成果为:wanghua 52
26、 23.50wanghua 52 23.50Press any key to continue2.#include#includestruct Worker char nameL15;姓名int age;年龄float pay;工资);void main()struct Worker x;char*t=,louting;int d=38;float f=493;strcpy(x.name,t);x.age=d;x.pay=f;x.age+;x.pay*=2;printf(%s%d%6.2fn,x.name,x.age,x.pay);)输出成果为:looting 39 986.00Press a
27、ny key to continue3.#include#includestruct Worker char name15;姓名int age;年龄float pay;工资;int Less(struct Worker rl,struct Worker r2)if(rl.age r2.age)return 1;else return 0;void main()struct Worker a4=HabcM,25,420,def,58,638,Mghi,49,560,Mjkln,36,375);struct Worker x=a01;int i;for(i=l;i 4;i+)if(Less(x,a
28、i )x=ai;printf(%s%d%6.2fn,x.name,x.age,x.pay);)输出成果为:def 58 638.00Press any key to continue四、写出下列每个函数功能1.#includeint SA(int a,int b)i f(ab)return 1;e l s e if(a=b)r e t u r n 0;e l s e r e t u r n -1;)函数功能为:依照实参a 不不大于、等于或不大于实参b,返回1,0或-12.#in cl u d e in t S C(in t a,in t b,in t c)if(a=b&a=c)r e t u
29、r n a;if(b=a&b=c)r e t u r n b;r e t u r n c;)函数功能为:返回实参a、b、c 中最大数3.d o u bl e S F(d o u bl e x,in t n)/n 为不不大于等于0整数d o u bl e p=l,s=l;f o r(i=l;i=n;i+)P*二 x;s+=p/(i+l);)r e t u r n s;)函数功能为:计算 x+x 2/2+x 3/3+.+x n/(n+1)值4.#in cl u d e in t S C(in t x)in t a=(in t)s q r t(x);in t i=2;w hil e(i=a)if (
30、x%i=z:O)br e ak;i+;)if(i=0)r e t u r n O;e l s e r e t u r n 1;)函数功能为:判断一种整数与否是素数5.#in cl u d e v o id t r an s(in t x)Ichar a 10;in t i=0,r e m;d o(r e m=x%16;x=x/16;if (r e m O)pr in t f(%c”,a 一i);pr in t f(n);函数功能为:将一种整数化成十六进制数1.#in cl u d e in t W B(in t a,in t n,in t x)f o r(in t i=0;in;i+)if(a
31、 i=x)r e t u r n 1;r e t u r n 0;)函数功能为:依照整型数组元素中与否能找到整数x,返回1或02.#in cl u d e in t W C(in t a,in t n,in t k)in t c=0;f o r(in t i=0;i=k)c+;r e t u r n c;)函数功能为:返回数组中前n个其值不不大于等于k元素之和3.#in cl u d e#in cl u d e#in cl u d e co n s t in t N=10;in t f f (in t x,in t y)in t z;pr in t f (猊+%d =x,y);s can f(
32、%d,&z);if(x+y-z)r e t u r n 1;e l s e r e t u r n 0;v o id m ain O(in t i,a,b,c=0;s r an d(t im e(0);初始化随机数系列f o r (i=0;iN;i+)a=r an d()%20+l;/r an d 90函数产生0-32767之间一种随机数b=r an d ()%20+1;c+=f f(a,b);)pr in t f (得分:%d n”,c*10);)函数功能为:函数f f让两个数相加,规定输入其和,判断成果与否对的。在主函数中用随机函数产生两个20以内随机整数,通过10次调用这个函数,算对一次
33、得10分,计算所得分4.in t f u n 6(in t m,in t n,in t b=2)if(m b&n b)r e t u r n m*n;e l s e if(m%b=O&n%b=O)r e t u r n b*f u n 6(m/b,n/b,b);e l s e r e t u r n f u n 6(m,n,+b);)函数功能为:此函数带有一种默认参数,若使用默认值,则通过递归调用,返回前2参数最小公倍数;不使用默认值时,若最后一种参数不不大于前2个参数,则返回前2参数之乘积;否则,通过递归调用,返回前2参数最小公倍数n 倍数。5.#in cl u d e#in cl u d
34、e v o id L I(in t n)in t *a=(in t *)电脑商场特别版2 m al l o c(n*s iz e o f (in t);in t i;f o r (i=0;i=0;i)pr in t f (%d ,*(a+i);p r i n t f C V);f r e e(a);)函数功能为:将从键盘输入n 个整数逆序输出6.#in cl u d e in t L K(d o u bl e a,in t n)d o u bl e s=0;in t i,m=0;f o r(i=0;in;i+)s+=a i;s/=n;f o r (i=0;i=s)m+;r e t u r n
35、m;)函数功能为:求不不大于数组元素之平均值各元素之和(前n个)应加入此4字,否则D 亦对必要强制类型转换!否则提示can n o t co n v e r t f r o m v o id *t o in t *五、依照下列每个题目规定编写程序1.编写一种函数,函数头格式为“v o i d f u n 4(ch a r *a ,i n t b)”,分别求出由字符指针a所指向字符串中包括每种十进制数字浮现次数,把记录成果保存在数组b 相应元素。#i n cl u d e v o i d f u n 4(ch a r*a,i n t b)d o(i f(*a =O&*a=,9 )b*a-4 8
36、+;w h i l e(*a+);)Av o i d m a i n ()!ch a r *a=8 8 8 ;i n t b1 0 =0;f u n 4 (a,b);f o r(i n t i=0;i 1 0;i+)p r i n t f(%d ,bi );*/2.编写一种函数,函数头格式为 d o u bl e Me a n (d o u bl e a M N ,i n t m ,i n t n)w,规定返回二维数组a m n 中所有元素平均值,假定在计算过程是采用变量v 存储平均值。#i n cl u d e co n s t i n t M=2,N=3;d o u bl e Me a n
37、(d o u bl e a M N,i n t m,i n t n )d o u bl e v=0;f o r(i n t i=0;i m;i+)f o r (i n t j=0;j n;j+)v+=a i j ;r e t u r n v/(m*n);)/*v o i d m a i n()!d o u bl e a 2 3 =1,2,3,4,5,6;p r i n t f (机 f n ,Me a n (a,2,3);*/3.编写一种递归函数“i n t FF(i n t a ,i n t n)”,求出数组a中所有元素n个元素之积并返回。#i n cl u d e i n t FF(i n
38、 t a ,i n t n)i n t m u l=l;i f(n=l)m u l*=a O;e l s e m u l=a n-l *FF(a,n-l);r e t u r n m u l;)Av o i d m a i n()Ii n t a 6 =l,2,3,4,5,6;p r i n t f C%d n,z,FF(a,6);*/4.编写一种主函数,运用w h i l e 循环,求出并显示满足不等式1+1/2+1/3+最小n值。#i n cl u d e v o i d m a i n()Id o u bl e s u m=0;i n t n=l;w h i l e(t r u e)+l
39、/n 5i f(s u m +1/(d o u bl e)n 5)br e a k;e l s es u m +二 1/(d o u bl e)n;n+;)p r i n t f (%d,%l f n,/,n,s u m);)5.编写一种主函数,求满足不等式2 2+4 2+n 2 1 0 0 0 最大n值,假定分别用i 和 s为取偶数值和累加值变量,并限定使用d o 循环编程。#i n cl u d e v o i d m a i n()!i n t s=0,i=2;d o(s+=i*i;i f(s+(i+2)*(i+2)=1 0 0 0)br e a k;e l s e i+=2;Jw h i l e(t r u e);p r i n t f (,zi=%d,s=%d”,i,s);)6.编写一种主函数,计算并输出n 值,其中n值由键盘输入。#i n cl u d e v o i d m a i n()i n t s=0,n;p r i n t f (请输入 n 值:);s ca n f(%d,&n);f o r(i n t i=l;i =n;i+)s+=i*i;p r i n t f (/zn=%d,s=%d,n,s);)
限制150内