2022年上海市计算机二级考试复习资料题目答案.docx
名师归纳总结 精品学习资料 - - - - - - - - - - - - - - -上海理工高校上海市运算机二级考试 第一部份:基础学问C 语言 复习资料题目 解答1. B 2. C 3. C 4. B 5. D 6. A 7. A 8. C 9. C 10. A 11. C 12. A 13. B 14. B 15. C 16. B 17. D 18.B 19. A 20. C 21. C 22. B 23. B 24. C 25. D 26. B 27. B 28. D 29. C 30. C 31. D 32. C 33. C 34. B 35. C 36. D 37. C 38. B 39. C 40. B 41. C 42. B 43. C 44. B 45. B 46. A 47. D 48. D 49. D 50. D 51. A 52. C 53. Sqrt x+ sqrt y/2/ x+ y or similar 54. The "234" is namely the string234 55. B 56.D 57.D 58. B 59. C 60.D 61.B 62. D 63.A 64.D 65.C 66.B 67.C 68. D 69. C 70.C 71.A 72.A 73.B 74.B 75.C 76. C 77.C 78.A 79.D 80.B 81.D 82.D 83.C 84.D 85.B 86.D 87.B 88.D 89.B 90.C 91.C 92.B 93.B 94.B 95. N+'0' 96. 0The x1400 moves is ex-, binary system 00010100 Bs or eight enter the 024 Qs of system 97. The first answer 65 ds or 41 hses or a the second answer 1 or not0 98. 85 99. 14 100. x=-60 其次部分 读程序 ,写结果1. 0,2,4,3 2. 3.142 3. 8 4. m/10%10*100+m/100*10+m%10 5. .uoy_era_woHHwaeyu 6. 7,3 7. 54656 8. A the = biggest number, b= is most the fraction 9. 1 10. 5,5 11. 9876543210 12. 1 3 5 7 9 13. j=0 m=4 The first k : 4 j=1 m=4 14. The second k : 5 15. Count=0, position=0 did not find out 细心整理归纳 精选学习资料 - - - - - - - - - - - - - - - 第 1 页,共 23 页 - - - - - - - - - 名师归纳总结 精品学习资料 - - - - - - - - - - - - - - -16. 0 -30 17. 输出 12 14 22 24 32 34,函数 s 是用插入法对数组 a 的前 n 个元素从小到大排序18. 输出 2 3 5 7 11 13 函数 p 的功能是求 k(包括 k )以内的质数 19. 输出 n=91, a=0, 10, 21, 31 20. 输出 Feith 21. 输出 72 56 38 4922. 81818181 23. k=5 24. 1 25. 9 26. 用递归函数实现 10->2 进制转换第三部分 程序改错1. #4 scanf“ %d” ,&x; #10 low=m+1 #11 high=m-1 2.#7 scanf“ %c” ,&stri; #8 break 3. #7 x , n #13 n=0 #15 x*xnx,n-1 4. #5 t=t*x/i #15 e=exx 5. #4 scanf%d” ,&x; #10 low=m+1 #11 high=m-1 6. #3 funstr #5 while *str+num.=0 num+;#9 char str10,*p=str; 7. #14 flag=0 ; #19 flag=1; i+; 8. #3 funcstr #5 while *str+num.=0 num+; #9 char str10,*p=str; 9. #5 n=n/10; #6 while .n; #12 scanf“ %ld” ,&n; 10. #10 else if x>am low=m+1; #11 else high=m-1; 11. #6 a=1.0; #8 a/=i; #9 e+=a; 12. #6 char *temp; #10 ifstrcmptemp,namei>0 #12 printf“ %s n” , temp ; 9 第 2 页,共 23 页 13. #3 stri.=0 && stri<=#4 stri>= 0细心整理归纳 精选学习资料 - - - - - - - - - - - - - - - - - - - - - - - - 名师归纳总结 精品学习资料 - - - - - - - - - - - - - - -#10 data = data*16 + n; 14. #3 int n1=0,n2=0, i; #6 if stri= #9#8 if stri>=0&&stri<=#9 n2+; 15. #5 i<N #7 sum=max=min=a0 #12 aver=floatsum-max-min/N-2 16. #7 scanf“ %c” ,&stri; #8 break 17. #7 x , n #13 n=0 #15 x*xnx,n-1 18. #4 double fundouble eps #8 while t>=eps #13 return 2*s; 19. #3 long funchar *p #17 num+=pi-48; 或 num+=pi-0; #29 printf“ %l n” ,n; /* 必需有 %ld */ 20. #9 aj<ai #15 bk>aj&&aj #18 aj=bk 21. #2 void printcolint *pN, int row, int col #6 printf“ tt%dn”,*p+i+col; #14 while col<0|col>N-1; 22. #2 char *s 或 *p.='0 #4 *p 或 *p.=0 #9 getsx; 23. #6 t=n%10; #10 n/=10; #11 whilen; 或 while n.=0; 24. #4 for j=0;j<=20-i;j+ #5 printf" " 25. #8 printf"n" #2 char *funchar *s,char *t #4 a=0;或 a=NULL; #8 if *r =*p r+; p+; 第四部分 编程序1. 设有链表结点的结构及变量如下:)struct st int num ; struct st *next; ; struct st *head, *p1 ,* p2; 编写函数 create,是个无参数函数, 创建链表,num=0终止,返回指针值, 指向首结点head;#define NULL 0 #define LEN sizeofstruct st struct st int num; 细心整理归纳 精选学习资料 - - - - - - - - - - - - - - - 第 3 页,共 23 页 - - - - - - - - - 名师归纳总结 精品学习资料 - - - - - - - - - - - - - - - struct st *next; ; int n=0; struct st *creat struct st *head, *p1,*p2; p1=p2=struct st *mallocLEN; scanf"%u,%d",&p1->num,&p1->score; head=NULL; while p1->num.=0 n=n+1; if n=1 head=p1; else p2->next=p1; p2=p1; p1=struct st *mallocLEN; scanf"%u,%d",&p1->num,&p1->score; p2->next=NULL; return head; 2. 对 N个数据 , 降序排列并输出 #define N 10 main int aN=1,5,0,3,-6,9,-8,7,2,4; int i,j,t; for i=0;i<N-1;i+ for j=i;j<N;j+ if ai<aj t=ai; ai=aj;aj=t; for i=0;i<N;i+ printf"%3d",ai; 3. 有二维数组如下1 2 3 4 5 6 7 8 9 10 11 12 现要求输入某元素所在的行和列,将其后的全部元素输出,必需用指针技术编程;例如,输入2 , 3 表示从其次行第三列元素7 开头输出其余元素7 8 9 10 11 12 main int a34=1,2,3,4,5,6,7,8,9,10,11,12; int *p,i,j; scanf"%d%d",&i,&j; p=&ai-1j-1; for ;p<&a00+12;p+ printf"%5d",*p; 4. 将一个磁盘文件的数据读到一个链表中去,队列或堆栈不论;要求如下:细心整理归纳 精选学习资料 磁盘文件放在A 盘的一级目录(即一级子文件夹)abc下,文件名是flile.dat 第 4 页,共 23 页 - - - - - - - - - - - - - - - - - - - - - - - - 名师归纳总结 精品学习资料 - - - - - - - - - - - - - - -链表结点的结构为 struct node char val; strct node *next; 工作用的指针名为 #include <stdio.h> h 和 p #include <string.h> struct node char val; struct node *next; ; struct node *createFILE *fp struct node *p,*h; char d; h=NULL; fp=fopen"a:abcfile.dat","r" while .feoffp fread&d,2,1,fp; p=struct node *mallocsizeofstruct node; p->val=d; p->next=h; h=p; fclosefp; printf"n" printlkh; return h; printlkstruct node *h struct node *p; printf"print link.n" p=h; while p printf"%c",p->val; p=p->next; main struct node *h,*p; FILE *fp; fp=fopen"rd_data","r" if fp=NULL printf"read data error.n" exit0; h=createfp; printlkh; exit0; 5. 试写一个函数 函数名为 f ,以十进制正整数n 为整型参数,函数的返回值是n 的位数(例: 第 5 页,共 23 页 278 是三位数);要求循环结构需用do_while掌握结构;细心整理归纳 精选学习资料 - - - - - - - - - - - - - - - - - - - - - - - - 名师归纳总结 精品学习资料 - - - - - - - - - - - - - - -int flong n int c=0; do c+; n=n/10; while n; return c; main long n; int num; scanf"%ld",&n; num=fn; printf"%ld has %d numbersn",n,num; 6将一个 n× n 的矩阵行列转置后输出,转置功能由函数reverse实现,试编程;#define N 3 void reversefloat xN,int n int i,j; float t; for i=0;i<n;i+ for j=i;j<n;j+ t=xij; xij=xji; xji=t; void prtablefloat xN,int n int i,j; for i=0;i<n;i+ for j=0;j<n;j+ printf"%10.4f",xij; printf"n" main int i,j; float aN=1,2,3,4,5,6,7,8,9; prtablea,N; reversea,N; prtablea,N; 7. 试用 do_while 掌握结构,运算以下级数的和 s=1+3+5+.+2i-1+ . 当累加到某项 s 的值大于 100 时,该项及以后各项不再累加,终止循环求和;main int s,i; s=0; i=0; do 细心整理归纳 精选学习资料 - - - - - - - - - - - - - - - 第 6 页,共 23 页 - - - - - - - - - 名师归纳总结 精品学习资料 - - - - - - - - - - - - - - - i+; s += 2*i-1; while s+2*i+1 <= 100 ; printf"sum=%d,i=%dn",s,i; 8编程序用于将原字符串按某种规律变成密码:即将字母A 变成 E,B 变成 F, ; W变成A,X 变成 B, , Z变成 D,对小写字母也是如此编码,标点符号、空格维护原状;#define N 4 #include <stdio.h> decode char c; while c=getchar.='n' if c>='a'&&c<='z'|c>='A'&&c<='Z' c=c+4; if c>'Z'&&c<='Z'+N|c>'z' c=c-26 ; printf"%c",c; main decode; 9. 编写一个带参数宏transF,输入一个华氏温度F,要求输出摄氏温度c ;公式是5F32c, 取 2 位小数;9#define transF F-32*5/9.0 main float c,F; scanf"%f",&F; c=transF; printf"F=%.2f, c=%.2fn",F,c; 10. 对非线性方程fx=0 ,可用牛顿迭代公式x n1x nfx n求解,要求方程式用函f'xn数名 f ,导函数 f 名为 f1 ,迭代函数名为g,假设函数f 和 f 已有定义,编写出迭代函数 gx ,精度小于10-5;float gfloat x float xi; do xi=x-fx/f1x; x=xi; while fabsfx>=1e-5; return x; 11. 编写 ptstar无参数函数打印如下图形,顶端的*定位在屏幕第25 字符位置,编出完整 第 7 页,共 23 页 程序,写出对ptstar的调用;细心整理归纳 精选学习资料 - - - - - - - - - - - - - - - - - - - - - - - - 名师归纳总结 精品学习资料 - - - - - - - - - - - - - - -123456789012345678901234567890 * * * * printstar int i,j; printf"123456789012345678901234567890n" for i=0;i<4;i+ for j=0;j<=23-i;j+ printf" " for j=0;j<2*i+1;j+ printf"*" printf"n" main printstar; 12.试写一个函数 函数名为f ,以正整数n 为整型参数,函数的返回值是以下级数的前n项的和 1+3+5+.+2n-1 要求用递归法运算级数的和;long fint n if n=1 return 1; else return 2*n-1+fn-1; main long sum; int n; scanf"%d",&n; sum=fn; printf"sum=%ldn",sum; 13. 本程序的主程序为:main int b=24,12,14,32,22,34; printf"%dn", fb,6; 其中调用函数f ,该函数有二个参数,依次为整型数组a 和整型数 n, 函数的返回值是数组的a0 到 an-1 中的最大值;试写出函数f ;要求循环结构需用for 掌握结构;int fint a,int n int i,max=-32768; for i=0;i<n;i+ if ai>max max=ai; return max; main int b=24,12,14,32,22,34; printf"%dn", fb,6; 细心整理归纳 精选学习资料 - - - - - - - - - - - - - - - 第 8 页,共 23 页 - - - - - - - - - 名师归纳总结 精品学习资料 - - - - - - - - - - - - - - - 14. 编写 sort函数,对N个 int型数据降升序排列, 该函数有2 个形参 :int a和 int n,采纳“ 冒泡法” 或“ 挑选法” 可任选;void sortint a,int n /* 挑选法 */ int i,j,sub,temp; for i=0;i<n-1;i+ sub=i; for j=i+1;j<n;j+ if asub>aj sub=j; ifi.=sub temp=ai; ai=asub; asub=temp; 15. 编写一个名为prime 的函数,以 m为整型参数,判别m是否为素数;如m为素数, 就返回 1;如 m不是素数,就返回0;#include <math.h> int primeint m int i,k; k=intsqrtm; for i= 2; i <= k ; i+ /* 或 i < m */ if m % i = 0 break; /* 或 . m % i */ if i > k return 1; /* 或 i=m */ else return 0; 16. 编写一个名为cntatoz的函数 , 输入一行字符, 统计其中的英文字母 个数 大小写不论 ;#include <stdio.h> main char ch; int n=0; printf"Input a string:n" while1 ch=getchar; ifch='n' break; ifch>='a'&&ch<='z'|ch>='A'&&ch<='Z' n+; printf"Number of letters is %d.n",n; 17. 设有 3 名同学成果存在如下的数组中:: int score34=65,57,70,60,90,87,50,81,90,65,100,98 编程输出有成果低于60 分的某同学全部成果 (按 C语言的规章,答应第0 行);main void searchfloat *p4,int n; float score34=65,57,70,60,58,87,90,81,90,99,100,98; searchscore,3; 细心整理归纳 精选学习资料 - - - - - - - - - - - - - - - 第 9 页,共 23 页 - - - - - - - - - 名师归纳总结 精品学习资料 - - - - - - - - - - - - - - - void searchfloat *p4,int n int i,j,flag; for j=0;j<n;j+ flag=0; for i=0;i<4;i+ if *p+j+i<60 flag=1; if flag=1 printf" No.%d fails,his scores are : n",j+1; for i=0;i<4;i+ printf" %6.1f",*p+j+i; printf"n" 18. 试用 do - while 掌握结构,运算以下公式的近似值2 4 6x x xs 12 1 . 4 3 . 6 5 .当运算某项的肯定值小于 0.0001 时,该项及以后各项不再累加,终止循环求和;float sumfloat eps float s = 0.0; int t = 1, i = 0; do s += t; i+; t = -t * x * x / 2 * i * 2 * i 1.0; while fabst >= eps; return s; 19. 试用 for 掌握结构,求数组aN 的前 k( k <= N )个元素的最大值和最小值;将统计结果分别存于变量 max和 min 中;float max,min; void mmaiint a,int k int i; max=min=a0; fori=1;i<k,i+ if max<ai max=ai; if min>ai min=ai; 20. 输入 10 个数,输出这 10 个数中仅显现一次的数 如输入: 12 36 72 36 87 99 87 87 12 35 就输出: 72 99 35 main int a10,i,j; for i = 0; i < 10; i+ scanf"%d", &ai ; for i = 0; i < 10; i+ 细心整理归纳 精选学习资料 - - - - - - - - - - - - - - - 第 10 页,共 23 页 - - - - - - - - - 名师归纳总结 精品学习资料 - - - - - - - - - - - - - - - for j = 0; j < 10; j+ if i.=j && ai=aj break; if j >= 10 printf "%d ", ai ; 21. 试设计一个函数,它能求出两个整型数的和与积,同时,再编写一个主程序 main,调用上述函数以验证该函数的功能与正确性;void testint a, int b, int *ps, int *pt *ps = a + b; *pt = a * b; main int a, b, s, t; scanf "%d%d", &a, &b ; test a, b, &s, &t ; printf "%d %dn", s, t ; 22.输入如干非0 实数,直到输入0 时停止,要求输入的实数最多不超过20 个,统计其中正数的个数,负数的个数;#include “stdio.h”main int n,posn,negn;double a; n=posn=0; printf“ Input real numbers:n”; scanf “ %lf ” ,&a; whilea.=0&&n<20 ifa>0 posn+; n+; if n<20 scanf“ %lf ” ,&a; negn=n-posn; printf“ posn=%d,negn=%dn”,posn,negn; 23. 输入一行字符,统计其中的英文字母个数;#include “stdio.h”main char ch; int n=0; printf“ Input a string:n”; while1 ch=getchar;ifch=n break; &&ch<=Z ifch>=a&&ch<=z|ch>= An+; printf“ Number of letters is %d.n” ,n; 细心整理归纳 精选学习资料 - - - - - - - - - - - - - - - 第 11 页,共 23 页 - - - - - - - - - 名师归纳总结 精品学习资料 - - - - - - - - - - - - - - -24. 编写完整的程序,用公式e111011.1.1.2.3n运算 e 的近似值,直到某项小于-7 为止;main double e = 1.0, u = 1.0; int n = 1; while u >= 1.0e-7 u = u/n; e = e+u; n = n+1; printf“ e=%8.6fn” , e; 25. 定义一个带参数的宏,其功能是求一个数的肯定值;#define ABSXx x<0 . 1*x : x 或类似26. 求出 11000 之间“ 水仙花数”;要求的水仙花数是个三位数,它的各位数字之和等于该数本身,例如:153=13+53+53,所以 153 是个水仙花数;#include <math.h> main int i,j,k,num; for i=1; i<=9; i+ for j=0; j<=9; j+ for k=0; k<=9; k+ num=powi,3+powj,3+powk,3; if num=