1、电大C语言形考册参考答案特别说明:题号后带有*的,该题有明显错误或疏漏,详细说明见形考册勘误C语言程序设计作业1(知识点覆盖范围:第1章至第3章)一、 选择题 1 2 3 4D 5B 6A 7 8 9 1011C 12 13 14 15* 16*D 17A二、 填空题 1; 2 3*/ 4*/ 5#include 6用户 7*函数头 8printf 9*void 1043 - 18 = 25 11warning 12error130x19(或0X19) 14n 15D 1672174 181 194 20421int 22double 23const 24初始化256 267 276.25 2
2、8429*+ x 30y = y + 1 31x 32x + y = 533a=b & b!=5 34假 35真 361037DataType 38整型常量表达式 39switch 40不停止4110 42break 43return 44*main三、 写出下列每个程序运行后的输出结果1 * 程序代码:#include void main() int x = 5; switch(2 * x - 3) case 4: printf(%d , x); case 7: printf(%d , 2 * x + 1); case 10:printf(%d , 3 * x - 1); break; de
3、fault:printf(%s, defaultn); printf(%sn, switch end.);运行结果:11 14 switch end.2 程序代码:#include void main() int i, s = 0; for(i = 1; i = 6; i +) s += i * i; printf(s=%dn, s);运行结果:s=913 程序代码:#include void main() int i, s1 = 0, s2 = 0; for(i = 0; i 10; i +) if(i % 2) s1 += i; else s2 += i; printf(%d %dn, s
4、1, s2);运行结果:25 204 程序代码:#include void main() int n = 10, y = 1; while(n -) y +; y +; printf(y=%dn, y);运行结果:y=215 程序代码:#include void main() int f, f1, f2, i; f1 = f2 = 1; printf(%d %d , f1, f2); for(i = 3; i = 10; i +) f = f1 + f2; printf(%d , f); if(i % 5 = 0) printf(n); f1 = f2; f2 = f; printf(n);运
5、行结果:1 1 2 3 58 13 21 34 556 * 程序代码:#include #include void main() int i, n; for(n = 2; n = 20; n +) int temp = (int)sqrt(n); / sqrt(n)求出n的平方根。然后取整 for(i = 2; i temp) printf(%d , n); printf(n);运行结果:2 3 5 7 11 13 17 197 程序代码:#include const int M=20;void main() int i, c2, c3, c5; c2 = c3 = c5 = 0; for(i
6、 = 1; i = M; i +) if(i % 2 = 0) c2 +; if(i % 3 = 0) c3 +; if(i % 5 = 0) c5 +; printf(%d %d %dn, c2, c3, c5);运行结果:10 6 48 程序代码:#include const int M=20;void main() int i, s; for(i = 1, s = 0; i 15; i +) if(i % 2 = 0 | i % 3 = 0) continue; printf(%d , i); s += i; printf(%dn, s);运行结果:1 5 7 11 13 37特别说明:
7、题号后带有*的,该题有明显错误或疏漏,详细说明见形考册勘误C语言程序设计作业2(知识点覆盖范围:第4章至第6章)一、 选择题 1C 2C 3C 4A 5C二、 填空题 18 232 360 4M * N 50M-1 60N-1 7BB 81 90 101 1111 12n + 113比较 14复制 1510 1619三、 写出下列每个程序运行后的输出结果1 (练习题4.2第1小题)程序代码:#include void main() int a10 = 12, 39, 26, 41, 55, 63, 72, 40, 83, 95 ; int i , i1 = 0, i2 = 0; for(i =
8、 0; i 10; i +) if(ai % 2 = 1) i1 +; else i2 +; printf(%d %dn, i1, i2);运行结果:6 42 * 程序代码:#include #include void main() int i; char * a5 = student, worker, cadre, soldier, peasant ; char * p1, * p2; p1 = p2 = a0; for(i = 0; i 0) p1 = ai; if(strcmp(ai, p2) 0) p2 = ai; printf(%s %sn, p1, p2);运行结果:worker
9、cadre3 * 程序代码:#include int a10 = 4, 5, 6, 15, 20, 13, 12, 7, 8, 9 ;void main() int i, s0, s1, s2; s0 = s1 = s2 = 0; for(i = 0; i 10; i +) switch(ai % 3) case 0: s0 += ai; break; case 1: s1 += ai; break; case 2: s2 += ai; break; printf(%d %d %dn, s0, s1, s2);运行结果:42 24 334 程序代码:#include void main() c
10、har a = abcdbfbgacd; int i1 = 0, i2 = 0, i = 0; while(ai) if(ai = a) i1 +; else if(ai = b) i2 +; i +; printf(%d %d %dn, i1, i2, i);运行结果:2 3 115 (在二维数组中找最大元素及其下标)程序代码:#include void main() int a34 = 1, 2, 7, 8 , 5, 6, 10, 6 , 9, 12, 3, 4 ; int m = a00; int ii = 0, jj = 0; int i, j; for(i = 0; i 3; i +
11、) for(j = 0; j m) m = aij; ii = i; jj = j; printf(%d %d %dn, ii, jj, aiijj);运行结果:2 1 126 (又是斐波纳契数列!)程序代码:#include void main() int a, b; for(a = 1, b = 2; b 50; ) printf(%d %d , a, b); a = a + b; b = a + b; printf(n);运行结果:1 2 3 5 8 13 21 34四、 写出下列每个函数的功能1 程序代码:#include int SA(int a, int b) if(a b) re
12、turn 1; else if(a = b) return 0; else return -1;函数功能:比较两个整数a和b的大小。如果a大于b则结果为1,a与b相等则结果为0,a小于b则结果为-12 程序代码:int SC(int a, int b, int c) if(a = b & a = c) return a; if(b = a & b = c) return b; return c;函数功能:返回a、b、c三个整数的最大值3 (注意s初值为1,等于x0/(0+1))程序代码:double SF(double x, int n) / n为大于等于0的整数 double p = 1,
13、s = 1; int i; for(i = 1; i = n; i +) p *= x; s += p / (i + 1); return s;函数功能:计算4 * 程序代码:#include int SG(int x) / x为大于等于2的整数 int a = (int)sqrt(x); / sqrt(x)取x的平方根 int i = 2; while(i = a) if(x % i = 0) break; i +; if(i = a) return 0; else return 1;函数功能:判断整数x是否为素数,是则返回1,不是返回05 程序代码:void trans(int x) ch
14、ar a10; int i = 0, rem; do rem = x % 16; x = x / 16; if(rem 0) printf(%c, a- i); printf(n);函数功能:以十六进制形式输出整数x五、 根据下列每个题目要求编写程序1void fun4(char * a, int b) int i; for(i = 0; i = 0 & * a = 9) b* a - 0 +; a +; 2double Mean(double aMN, int m, int n) double v = 0; int i, j; for(i = 0; i m; i +) for(j = 0;
15、j n; v += aij +); return v / m / n;3int FF(int a, int n) return (n = 0)? 1 : an - 1 * FF(a, n - 1); 4*(题目居然没有给出不等式!可能是1 + 1/2 + 1/3 + . + 1/n 5)#include void main() int i=0; double s=0; while(s5) s+=1.0/+i; printf(n=%dn, i);5*(不等式应当是22+42+.+n21000)#include void main() int i=0; /用i作为依次取值偶数的变量 int s=0
16、; /用s作为累加变量 do i+=2; s+=i*i; while(s1000); printf(n=%dn, i-2);6*(应当是计算12+22+.+n2)#include void main() int n, i, sum; printf(请输入正整数n:); scanf(%d, &n); if(n = 0) printf(应输入正整数!n); return; for(i = 1, sum = 0; i = n; sum += i * i, i +); printf(平方和为:%dn, sum);特别说明:题号后带有*的,该题有明显错误或疏漏,详细说明见形考册勘误C语言程序设计作业3(
17、知识点覆盖范围:第6章)一、 选择题 1*C 2*C 3B 4B 5C 6B 7C 8B 9B 10C11B 12D 13D 14D 15*A 16D二、 填空题 1函数体 20 3static 4递归 5头 6函数体 725 846 946 104*i 11(char *)p 12int*13&p 14* p 150三、 写出下列每个程序运行后的输出结果1 程序代码:#include void SB(char ch) switch(ch) case A: case a: printf(WW ); break; case B: case b: printf(GG ); break; case
18、C: case c: printf(PP ); break; default: printf(BB ); break; void main() char a1 = b, a2 = C, a3 = f; SB(a1); SB(a2); SB(a3); SB(A); printf(n);运行结果:GG PP BB WW2 程序代码:#include #include double SD(int a, int b, char op) double x; switch(op) case +: x = a + b; break; case -: x = a - b; break; case *: x =
19、 a * b; break; case /: if(b) x = (double)a/b; else exit(1); break; default: printf(运算符错!n); exit(1); return x;void main() int x = 20, y = 8; printf(%3.2lf , SD(x, y, -); printf(%3.2lf , SD(x, y, *); printf(%3.2lfn, SD(x + y, y, /);运行结果:12.00 160.00 3.503 程序代码:#include void WF(int x, int y) x = x + y
20、; y = x + y; printf(subs: x, y = %d, %dn, x, y);void main() int x = 18, y = 23; printf(main: x, y = %d, %dn, x, y); WF(x, y); x = 2 * x; printf(main: x, y = %d, %dn, x, y);运行结果:main: x, y = 18, 23subs: x, y = 41, 64main: x, y = 36, 234 (将字符串逆序。注意for循环只到一半长度就前后交换完了。如果循环整个长度则会前后交换两次,最终顺序不变)程序代码:#inclu
21、de #include void fun(char ss);void main() char s15 = 567891234; fun(s); printf(%sn, s);void fun(char ss) int i, n = strlen(ss); for(i = 0; i n / 2; i +) char c = ssi; ssi = ssn - 1 - i; ssn - 1 - i = c; 运行结果:4321987655 (插入排序法降序排序。模拟玩扑克时起牌的过程进行排序:手上拿的牌(a0ai - 1)已经排好序,拿起一张牌ai,从ai - 1开始一张张地找,如果比ai小则把这张
22、牌aj向右挪,直到当前的牌aj不比ai小了,则将ai放到aj的右边。注意在开始找之前要先把ai保存起来,因为把ai - 1向右挪的时候就会执行ai=ai - 1,从而改变ai的值。插入排序法升序排序见练习题6.3第2小题。)程序代码:#include void InsertSort(int a, int n) int i, j, x; for(i = 1; i = 0; j -) / 为x顺序向前寻找合适的插入位置 if(x aj) aj + 1 = aj; else break; aj + 1 = x; void main() int i; int a6 = 20, 15, 32, 47,
23、36, 28 ; InsertSort(a, 6); for(i = 0; i 6; i +) printf(%d , ai); printf(n);运行结果:47 36 32 28 20 156 程序代码:#include void main() int a8 = 3, 5, 7, 9, 11, 13, 15, 17 ; int i, * p = a; for(i = 0; i 8; i +) printf(%5d, * p +); if(i + 1) % 4 = 0) printf(n); 运行结果: 3 5 7 9 11 13 15 177 程序代码:#include int LA(in
24、t * a, int n) int i, s = 0; for(i = 0; i n; i +) s += ai; return s;void main() int a = 5, 10, 15, 20, 25, 30 ; int b = LA(a, 4); int c = LA(a + 2, 3); printf(%d %dn, b, c);运行结果:50 608 程序代码:#include int LB(int * a, int n) int i, s = 1; for(i = 0; i n; i +) s *= * a +; return s;void main() int a = 1, 2, 3, 4, 2, 4, 5, 2 ; int b = LB(a, 4) + LB(&a3, 4); printf(b=%dn, b);运行结果:b=184四、 写出下列每个函数的功能1 * 程序代码:int WB(int a, int n, int x) int i; for(i = 0;
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2