250 likes | 424 Views
2002 年上海市高等学校 计算机等级考试 二级( C 语言). 试题一( 28 分,每小题 4 分). ⑴ 试写出一个 C 表达式,计算一元二次方程 ax 2 +bx+c=0 的一个根 ⑵ 执行下列程序段后,数组 a 的成员的值各为多少 ? int a[4]={1,3,5,7}; a[3]=--a[0]?++a[1]:a[2]--;. (-b+sqrt(b*b-4*a*c))/(2*a). a[0] =0 a[1]=3 a[2]=4 a[3]=5. ⑶ 设有如下程序: #include <stdio.h>
E N D
2002年上海市高等学校 计算机等级考试 二级(C语言)
试题一(28分,每小题4分) ⑴ 试写出一个C表达式,计算一元二次方程 ax2+bx+c=0的一个根 ⑵ 执行下列程序段后,数组a的成员的值各为多少? int a[4]={1,3,5,7}; a[3]=--a[0]?++a[1]:a[2]--; (-b+sqrt(b*b-4*a*c))/(2*a) a[0] =0 a[1]=3 a[2]=4 a[3]=5
⑶ 设有如下程序: #include <stdio.h> void main() { int a, c; float b; c=(a=2*3, a+(int)(b=20/3)); printf("%d\t%f\t%d\n",a,b,c); } 试写出程序的输出结果。 6 6.000000 12
库函数: strcat(char *str1, char *str2) ⑷ 设有如下程序段: char s1[20]="P4CPU"; char s2[]="DVD+17\"CRT+56KMODEM"; s2[10]='\0'; strcat(s1,s2+3); puts(s1); 写出执行后的输出结果。 ⑸ 设有如下宏定义: #define round_2(x) (int)(x*100+0.5)/100.0 试说明该宏定义的功能并写出语句 printf("%f",round_2(3.1416)); 的输出结果。 P4CPU+17"CRT 功能:对实数x小数点后第三位作四舍五入。 结果:3.140000
⑹ 设有如下程序: #include <stdio.h> void main() { int x; printf("%d\t",x=12&7); printf("%d\t",x=6^9); printf("%d\t",x=012|5); printf("%d\t",x=7&3|5); } 试写出其输出结果。 4 15 15 7
⑺ 设有下列变量定义: int a[5]={2,3,4,5,6}; int *p=a+2; 求表达*(p++)*a[3]的值。 20
试题二(12分,每小题6分) 阅读下列程序,写出程序的输出结果。 ⑴ 【程序2.1】 #include <stdio.h> void main() { int a[6]={0,1,2,3,4}; int i,c=0,d=0; for(i=0;i<=3;i++) { a[i+1]=a[i]+c+d+1; c=c+d+d+3; d=d+3; printf("%d\n",a[i]); } } } 0 1 8 27
⑵ 【程序2.2】 #include <stdio.h> int count, position, data; void search(int *p, int n, int st) { if(*p==data) { count++; position=st; } if(n>1) search(p+1,n-1,st+1); } void main() { int a[5]={10,30,50,30,20}; count=0; data=30; search(a,5,1); printf("count=%d,position=%d\n",count,position); } count=2,position=4
试题三(12分,每小题6分) 该错。下列函数或程序都有三个错误,按题中的计 算要求,纠正所有错误,并以“将#XX行改为YYY” 的形式进行解答。下列代码左边的#1、#2、…… 是附加的行号。 ⑴ 16进制数可以由数字0到9和大写字母A至F组成的 字符串表示,函数3.1的功能是将用字符串表示的 16进制数转化为十进制数,如将2A转化为42,若 字符串中出现其他符号则返回-1。 【函数3.1】
#3 int data=0, i, n; #1int hexvalue( char *str) #2 { #3 int data, i, n; #4 for ( i = 0; str[i]!=’\0’; i++ ) #5 { #6 if ( str[i]<=’9’ ) #7 n = str[i] – ‘0’; #8 else if ( str[i]>=’A’ && str[i]<=’F’ ) #9 n = str[i] – ‘A’ + 10; #10 else #11 return –1; #12 data = data*10 + n; #13 } #14 return data; #15 } #6 if (str[i]>=’0’ && str[i]<=’9’ ) #12data=data*16 + n;
⑵ 程序3.2计算数列2/1,3/2,5/3,8/5,…,f10/f9之和,其中 fn由f0=1,f1=2,fn=fn-1+fn-2(n>1)定义。 【程序3.2】
#2 double series_sum(n) #1#include <stdio.h> #2 series_sum(n) #3 { int a=2, b=1, c, k; #4 double s=0.0; #5 for(k=1; k<=n; k++) #6 { s=s+a/b; #7 c=a; a+=b; b=c;} #8 return; #9 } #10 void main() #11 { int n=10; #12 printf("The sum is:%lf\n",series_sum(n)); #13 } #6 { s=s+ (double)a/b; #8 return s ;
试题四(18分,每小题6分) 按指定的要求编写程序段。 ⑴ 编写一个程序,打印出所有的“梅花数”。所谓“梅 花数”是指一个五位数,其各位数字的五次方和等 于该数本身。例如:54748是一个“梅花数”,因为 54748=55+45+75+45+85。 ⑵ 定义一个函数,统计3行4列的整数二维数组中有 多少个正数、多少个负数,多少个零,并返回统 计结果。 ⑶ 编写一个程序求数组a的最大数和第二最大数并分 别存放于a[0]和a[1]中。假设数组中元素各不相同。
⑴ 参考解1 #include "math.h" int digital(long x,int n) { if(n==1) return x%10; else digital(x/10,n-1);} void main() { long i, s; int j; for(i=10000; i<100000; i++) { for(s=0, j=1; j<=5; j++) s=s+pow( digital( i, j ), 5 ); if(i==s) printf("%ld\n",i); } }
⑴ 参考解2 #include "math.h" void main() { long i, s, t; int j; for(i=10000;i<100000;i++) { for(s=0, t=i, j=1; j<=5; j++, t/=10) s+=pow(t%10, 5); if(i==s) printf("%ld\n",i); } }
⑵ 参考解1 void stat(int a[][4],int *plus, int *negative , int *zero) { int i,j; *plus=*negative=*zero=0; for(i=0;i<3;i++) for(j=0;j<4;j++) if (a[i][j]>0) (*plus)++; else if (a[i][j]<0) (*negative)++; else (*zero)++; }
⑵ 参考解2 void stat(int a[][4], int b[] ) { int i,j; b[0]=b[1]=b[2]=0; for(i=0;i<3;i++) for(j=0;j<4;j++) if (a[i][j]>0) b[0]++; else if (a[i][j]<0) b[1]++; else b[2]++; }
⑶ 参考解 void main() { int a[10], i, j, t; for(i=0; i<10; i++ ) scanf ("%d",&a[i]); for(i=0;i<2;i++) { for(j=i+1;j<10;j++) if(a[i]<a[j]) { t=a[i]; a[i]=a[j]; a[j]=t;} } printf("max1=%d,max2=%d\n",a[0],a[1]); }
试题五(15分) 阅读下列问题描述和相应的程序,把应填入其中 (n) 处的内容写在答卷纸的对应栏内。 【问题描述】 本程序为简易密码变换,输入字符串,将其中的 小写字母替换为英文字母表中该字母之后的第5 个字母。若替换的字母超过'z',则循环到'a'。例 如,将'a'替换为'b'。 实例:字 符 串:Have*a*Good*Test! 变换结果:Hfaj*f*Gtti*Tjxy!
【程序5】 #include <stdio.h> int count, position, data; void code(char *scr, char *dest) { char ch; while( ch= (1) ) { if( (2) ) { ch+=5; if(ch>'z') ch= (3) ; } *dest=ch; dest++; } *dest= (4) ; } *(src++) ch>='a'&&ch<='z' (3)ch-'z'+'a'-1或ch-26 '\0'或0
void main() { char buf[100], inb[100]; scanf("%s",inb); code( (5)); printf("%s",buf); } inb , buf
试题六(15分) 阅读下列问题描述和相应的程序,把应填入其中 (n) 处的内容写在答卷纸的对应栏内。 【问题描述】 本程序主函数先打开当前目录中名为st.dat的文件, 调用函数creat()读入文件st.dat中的整数并构造一个 链表,再调用函数copy(),由刚才建立的链表复制 生成一个新链表,新链表按整数值从小到大(单调 不减)顺序链接。然后顺序输出原链表和新链表的 元素。
【程序6】 #include "stdio.h" #include "stdlib.h" #define fname "a.txt" struct node { int val; struct node *next;}; struct node *creat(FILE *fp) { struct node *p,*h; int d; h=NULL; while( (1) ) { p=(struct node *)malloc(sizeof(struct node)); p->val=d; p->next= (2) ; h=p; } return h; } (1)fscanf(fp,"%d",&d)!=EOF h
struct node *copy(struct node *h) { struct node *p,*q,*u,*v,*h1=NULL; for(p=h;p;) { q=(struct node *)malloc(sizeof(struct node)); q->val=p->val; v=h1; while( (3) ) { u=v; v=v->next; } if(v==h1) h1=q; else (4) ; q->next=v; p=p->next; } return h1; } (3)v&&q->val > v->val u->next=q
void main() { struct node *h1,*h2,*p; FILE *fp; fp=fopen(fname,"r"); h1=creat( (4) ); for(p=h1;p;p=p->next) printf("%6d",p->val); printf("\n"); h2=copy(h1); for(p=h2;p;p=p->next) printf("%6d",p->val); printf("\n"); } fp