【计算机类职业资格】初级程序员下午试题-16及答案解析.doc

上传人:confusegate185 文档编号:1330079 上传时间:2019-10-17 格式:DOC 页数:11 大小:52KB
下载 相关 举报
【计算机类职业资格】初级程序员下午试题-16及答案解析.doc_第1页
第1页 / 共11页
【计算机类职业资格】初级程序员下午试题-16及答案解析.doc_第2页
第2页 / 共11页
【计算机类职业资格】初级程序员下午试题-16及答案解析.doc_第3页
第3页 / 共11页
【计算机类职业资格】初级程序员下午试题-16及答案解析.doc_第4页
第4页 / 共11页
【计算机类职业资格】初级程序员下午试题-16及答案解析.doc_第5页
第5页 / 共11页
点击查看更多>>
资源描述

1、初级程序员下午试题-16 及答案解析(总分:18.00,做题时间:90 分钟)一、B试题一/B(总题数:1,分数:3.00)1.阅读以下说明和流程图回答问题,将解答填入对应栏。 说明 “直接插入法”排序是一种 N2运算量的例程,只能用在 N 较小的时候,其方法是:挑出第二个数将它按与第一个数大小的顺序插入,然后挑出第三个数将它按大小顺序插入到前两个数中,如此下去,一直到最后一个也插入。 注:流程中循环开始的说明按照“循环变量:循环初值,循环终值,增量”格式描述。 问题 将流程图的(1)(5)处补充完整。(分数:3.00)填空项 1:_二、B试题二/B(总题数:1,分数:3.00)2.阅读以下函

2、数说明和 C 语言函数,将应填入U (n) /U处的字句写在对应栏内。 说明 1 函数int factors(int n)的功能是判断整数 n(n=2)是否为完全数。如果 n 是完全数,则函数返回 0,否则返回-1。 所谓“完全数”是指整数 n 的所有因子(不包括 n)之和等于 n 自身。例如:28 的因子为1,2,4,7,14,而 28=1+2+4+7+14,因此 28 是“完全数”。 C 函数 1 int factors(int n) int i,S; for(i=l,s=0;i=n/2;i+) if(n%i=O) U(1) /U; if(U (2) /U)return 0; rerurn

3、 -1; 说明 2 函数 int maxint(int a,int k)的功能是用递归方法求指定数组中前 k 个元素的最大值,并作为函数值返回。 C 函数 2 int maxint(int a,int k) int t; if(U (3) /U)return U(4) /U; t=maxint(a+1, U(5) /U)j return(a0t) ? a0 :t; (分数:3.00)填空项 1:_三、B试题三/B(总题数:1,分数:3.00)3.阅读以下函数说明和 C 语言函数,将应填入U (n) /U处的字句写在对应栏内。 说明 该程序从正文文件 test.txt 中读入一批整数,并将它们按

4、照递增的顺序存放在一个链表中。其中,函数struct Link*insertChain(struct Link*head,structLink*k)用来寻找结点 k 在链表 head 中的插入位置,并插入该结点。 C 程序 #include stdio.h#include stdlib.h struct Link int number; struct Link *next; struct Link *insertChain(struct Link *head,struct Link *k); VOid main() struct Link *ptr, *head; FILE *fd; int

5、hum; if(fd=fopen(“test.txt“,“r“)=NULL) print(“Cannot open this file!/n“); return; head=NULL; while(fscanf(fd,“%d“,hum)=1) ptr=U (1) /U; if(!ptr) return; ptr-number=hum; ptr-next=NULL; head=insertChain(head,ptr); U (2) /U; return; struct Link *insertChain(struct Link *head,struct Link *k) struct Link

6、 *ptr,*u; ptr=head; while(ptr k k-numberptr-number) u=ptr; U(3) /U if(ptr = head) head=k; else U(4) /U; U(5) /U; return head; (分数:3.00)填空项 1:_四、B试题四/B(总题数:1,分数:3.00)4.阅读以下函数说明和 C 语言函数,将应填入U (n) /U处的字句写在对应栏内。 说明 这是一个模拟渡口管理的算法。某汽车轮渡口,过江渡船每次能载 10 辆车过江。过江车辆分为客车类和火车类,上船有如下规定:同类车先到先上船,客车先于货车上渡船,且每上 4 辆客车,

7、才允许上一辆货车;若等待客车不足 4 辆,则以货车代替,若无货车等待则允许客车都上船。 程序中用到的函数有 enqueue(queue*sq,elemtype *x)在队列 sq 中入队一个元素x;outqueue(queue *sq,elemtype *x)在队列 sq 中出队一个元素,并将其值赋给 x;empty(queue *sq)判断队列 sq 是否为空队,若为空,返回 1;否则返回 0。 C 程序 #includestdio.h void pass() queue bus,truct; /*bus 表示客车队列,truck 表示货车队列*/ char ch; int n,tag; /

8、 * n 为车号,tag 为标志,tag=0 表示客车,tag=1 表示货车 */ intcount=0,countbus=0,counttruck=0; / *分别表示上渡船汽车数、客车数、货车数 */ while(1) printf(“输入命令: /n“); Scanf(“%c“, switch(ch) casee: caseE: printf(“车号: /n“); Scanf(“%d“, printf(“客车/货车(0/1): /n“); scanf(“%d“, if(U (1) /U) enqueue( else enqueue( break; casei: caseI: while(

9、count10) if(U (2) /U printf(“上船的车号为: /n“); count+; U(3) /U; eise if(U (4) /U) /*货车出队 */ countbus=0; outqueue( printf(“上船的车号为: /n“); count+; counttruck+; else if(empty( outqueue( printf(“没有 10 辆车排队轮渡/n“); count+; countbus+; else printf(“没有 10 辆车排队轮渡/n“); retUrn; break; caseq: caseQ:break; if(ch=q | c

10、h=Q) break; (分数:3.00)填空项 1:_五、B试题五/B(总题数:1,分数:3.00)5.阅读以下说明和 C+程序,将应填入U (n) /U处的字句写在对应栏内。 说明 下面程序输出一个矩形面积,以及矩形区域上的假想的作物产量。 C+程序 #include iostream.h class crop_assessment int actual_crop; int ideal_crop; public: void set(int in_actual,int in_ideal) actual crop=in_actual; ideal_crop=in_ideal; int get_

11、actual_crop(void)U (1) /U; int get_ideal_crop(void)U (2) /U;) ; Class lot_size int length; int width; U (3) /U crop; public: void set(int 1,int w,int a,int i) length=1; width=w; crop.set(a,i); int get_area(void)return length*width; int get_data(void)returnU (4) /U; int get_data2(void)freturnU (5) /U

12、; int main() Los_size small,medium; small.set(5,5,5,25); medium.set(10,10,10,50); cout“For a small lot of area“smallget_area()“/n”;cout“the actual crops are$“small.get_data2()“/n“;cout“and ideal crops are$”small.get_data()“/n“;cout“For a medium Lot of area“medium.get area():/n”;cout“the actual crops

13、 are$“medium.get_data2()“/n“;cout“and ideal crops are$“medium.get_data()“/n“;return 0;(分数:3.00)填空项 1:_六、B试题六/B(总题数:1,分数:3.00)6.阅读以下说明和 Java 程序,将应填入U (n) /U处的字句写在对应栏内。 说明 下面程序输出一个矩形面积,以及矩形区域上的假想的作物产量。 Java 程序 public class MainJava public static void main(String args) Lot_size small=new Lot_size(); Lo

14、t_size medium=new Lot_size(); small.set(5,5,5,25); medium.set(10,10,10,50); System.out.println(“For a small lot of area“ +small.get_area()+“/n“); System.out.println(“the actual crops are $“ +small.get_data2()+“/n“); System.out.println(“and ideal crops are $“ +small.get data()+“/n“); System.out.print

15、ln(“For a medium lot of area“ +medium.get_area()+“/n”); System.out.println(“the actual crops are $“ +medium.get_data2()+“/n“); System.out.println (“and ideal crops are $“ +medium.get_data()+“/n“); class Crop_assessment private int actual_crop; private int ideal_crop; public void set(int in_actual,in

16、t in ideal) actual_crop=in_actual; ideal_crop=in_ideal; public int get_actual_crop()return U(1) /U; public int get_ideal_crop()(return U(2) /U;class Lot_size private int length; private int width; private Crop_assessment crop=U (3) /U; public void set(int 1,int W,int a,int i) length=1; width=W; crop

17、.set(a,i); public int get_area()return length*width; public int get_data()freturnU (4) /U; public int get_data2()(returnU (5) /U;(分数:3.00)填空项 1:_初级程序员下午试题-16 答案解析(总分:18.00,做题时间:90 分钟)一、B试题一/B(总题数:1,分数:3.00)1.阅读以下说明和流程图回答问题,将解答填入对应栏。 说明 “直接插入法”排序是一种 N2运算量的例程,只能用在 N 较小的时候,其方法是:挑出第二个数将它按与第一个数大小的顺序插入,然后

18、挑出第三个数将它按大小顺序插入到前两个数中,如此下去,一直到最后一个也插入。 注:流程中循环开始的说明按照“循环变量:循环初值,循环终值,增量”格式描述。 问题 将流程图的(1)(5)处补充完整。(分数:3.00)填空项 1:_ (正确答案:1,n-1,1; (2) aj; (3) ajai; (4) ai+1=ai; (5) ai+1=a;)解析:解析 本题目考查流程图。 题目中已经给出了直接插入法排序的算法,由于该算法是从数组中第二个数起,取出并与前面的数进行排序,直到数组中最后一个数排序完成,所以,循环变量初值为 1,终值为 N-1,增量为 1,按照题目中要求的格式,则为“1,N-1,1

19、”。 由题目中的算法可知,我们要将取出来的数同排在其前面的数做比较,并插入,所以,首先要把取出来的数赋给一个变量,即(2)填入“aj”。然后从第 j-1 个数开始,如果 aiaj,那么我们就将 aiN 移一位,以便空出一个位置来插入 aj,所以(4)填入“ai+1=ai”这样一直进行到 aiaj或者 i0,这时,我们就找到了 aj要插入的位置,可以将 aj插入,即(3)填入“ajai”,(5)填入“ai+1=a”。二、B试题二/B(总题数:1,分数:3.00)2.阅读以下函数说明和 C 语言函数,将应填入U (n) /U处的字句写在对应栏内。 说明 1 函数int factors(int n)

20、的功能是判断整数 n(n=2)是否为完全数。如果 n 是完全数,则函数返回 0,否则返回-1。 所谓“完全数”是指整数 n 的所有因子(不包括 n)之和等于 n 自身。例如:28 的因子为1,2,4,7,14,而 28=1+2+4+7+14,因此 28 是“完全数”。 C 函数 1 int factors(int n) int i,S; for(i=l,s=0;i=n/2;i+) if(n%i=O) U(1) /U; if(U (2) /U)return 0; rerurn -1; 说明 2 函数 int maxint(int a,int k)的功能是用递归方法求指定数组中前 k 个元素的最大

21、值,并作为函数值返回。 C 函数 2 int maxint(int a,int k) int t; if(U (3) /U)return U(4) /U; t=maxint(a+1, U(5) /U)j return(a0t) ? a0 :t; (分数:3.00)填空项 1:_ (正确答案:s+=i (2) n=s (3) k-1 或 k-1=0 (4) a0或 *a 或 ak-1)解析:(5) k-1 或-k解析 对于函数 1,是判断整数 n(n=2)是否为完全数。首先用 for 循环求该整数的所有因子之和,所以(1)填“s+=i”若其和等于整数本身,则为完全数,返回值为 0,则(2)填“n

22、=s”;否则返回值为-1。 对于函数 2,是用递归方法找出数组中的最大元素。该递归的出口条件为 k=1,即(3)填“k=1”或“k-1=0”:只有一个数时,它本身就是最大的,(4)填“a0”或“ *a”或“ak-1”;对于多个数的情况,在剩下的 k-1 个元素中找到最大的,并与首元素值比较,返回最大的一个,所以(5)填“k-1”或“-k”。三、B试题三/B(总题数:1,分数:3.00)3.阅读以下函数说明和 C 语言函数,将应填入U (n) /U处的字句写在对应栏内。 说明 该程序从正文文件 test.txt 中读入一批整数,并将它们按照递增的顺序存放在一个链表中。其中,函数struct Li

23、nk*insertChain(struct Link*head,structLink*k)用来寻找结点 k 在链表 head 中的插入位置,并插入该结点。 C 程序 #include stdio.h#include stdlib.h struct Link int number; struct Link *next; struct Link *insertChain(struct Link *head,struct Link *k); VOid main() struct Link *ptr, *head; FILE *fd; int hum; if(fd=fopen(“test.txt“,“

24、r“)=NULL) print(“Cannot open this file!/n“); return; head=NULL; while(fscanf(fd,“%d“,hum)=1) ptr=U (1) /U; if(!ptr) return; ptr-number=hum; ptr-next=NULL; head=insertChain(head,ptr); U (2) /U; return; struct Link *insertChain(struct Link *head,struct Link *k) struct Link *ptr,*u; ptr=head; while(ptr

25、 k k-numberptr-number) u=ptr; U(3) /U if(ptr = head) head=k; else U(4) /U; U(5) /U; return head; (分数:3.00)填空项 1:_ (正确答案:(struct Link *)malloc(sizeof (struct Link) (2) fclose(fd))解析:(3) ptr=ptr-next 或 ptr=u-next (4) u-next=k (5) k-next=ptr解析 本题采用文件的输入方式,其主要思路:打开文件;从文件读入一个整数,动态申请一个结点;将结点插入到以 head 为头指针

26、的链表中;反复执行和直到文件结束;关闭文件。 通过上述分析,不难得到(1)为申请结点,应该填“(struct Link *)malloc(sizeof(structLink)”;(2)为关闭文件,即 fclose(fd)。在插入结点时,让 u 指向插入结点 ptr 之前,因此(3)为 u 的下一个结点,填“ptr=ptr-next”或“ptr=u-next”。插入结点时分为在头结点插入和 u 之后插入两种情况。(4)为在 u 之后插入的情况,应该填“u-next=k”,(5)填“k-next=ptr”。四、B试题四/B(总题数:1,分数:3.00)4.阅读以下函数说明和 C 语言函数,将应填入

27、U (n) /U处的字句写在对应栏内。 说明 这是一个模拟渡口管理的算法。某汽车轮渡口,过江渡船每次能载 10 辆车过江。过江车辆分为客车类和火车类,上船有如下规定:同类车先到先上船,客车先于货车上渡船,且每上 4 辆客车,才允许上一辆货车;若等待客车不足 4 辆,则以货车代替,若无货车等待则允许客车都上船。 程序中用到的函数有 enqueue(queue*sq,elemtype *x)在队列 sq 中入队一个元素x;outqueue(queue *sq,elemtype *x)在队列 sq 中出队一个元素,并将其值赋给 x;empty(queue *sq)判断队列 sq 是否为空队,若为空,

28、返回 1;否则返回 0。 C 程序 #includestdio.h void pass() queue bus,truct; /*bus 表示客车队列,truck 表示货车队列*/ char ch; int n,tag; / * n 为车号,tag 为标志,tag=0 表示客车,tag=1 表示货车 */ intcount=0,countbus=0,counttruck=0; / *分别表示上渡船汽车数、客车数、货车数 */ while(1) printf(“输入命令: /n“); Scanf(“%c“, switch(ch) casee: caseE: printf(“车号: /n“); S

29、canf(“%d“, printf(“客车/货车(0/1): /n“); scanf(“%d“, if(U (1) /U) enqueue( else enqueue( break; casei: caseI: while(count10) if(U (2) /U printf(“上船的车号为: /n“); count+; U(3) /U; eise if(U (4) /U) /*货车出队 */ countbus=0; outqueue( printf(“上船的车号为: /n“); count+; counttruck+; else if(empty( outqueue( printf(“没有

30、 10 辆车排队轮渡/n“); count+; countbus+; else printf(“没有 10 辆车排队轮渡/n“); retUrn; break; caseq: caseQ:break; if(ch=q | ch=Q) break; (分数:3.00)填空项 1:_ (正确答案:tag=O (2) count4 (3) countbus+ (4)empty( int ideal_crop; public: void set(int in_actual,int in_ideal) actual crop=in_actual; ideal_crop=in_ideal; int get

31、_actual_crop(void)U (1) /U; int get_ideal_crop(void)U (2) /U;) ; Class lot_size int length; int width; U (3) /U crop; public: void set(int 1,int w,int a,int i) length=1; width=w; crop.set(a,i); int get_area(void)return length*width; int get_data(void)returnU (4) /U; int get_data2(void)freturnU (5) /

32、U; int main() Los_size small,medium; small.set(5,5,5,25); medium.set(10,10,10,50); cout“For a small lot of area“smallget_area()“/n”;cout“the actual crops are$“small.get_data2()“/n“;cout“and ideal crops are$”small.get_data()“/n“;cout“For a medium Lot of area“medium.get area():/n”;cout“the actual crop

33、s are$“medium.get_data2()“/n“;cout“and ideal crops are$“medium.get_data()“/n“;return 0;(分数:3.00)填空项 1:_ (正确答案:return actual_crop (2) return ideal_crop (3) Crop_assessment)解析:(4) cropget_ideal_crop() (5) crop.get_actual_crop() 解析 本题以 C+语言为载体,考查面向对象程序设计中的几个重要概念类的嵌套及函数的使用。 本题的功能是通过已定义的粮食收成类定义了一个计算特定矩形域

34、上粮食收成的类,在主函数中定义了两个对象,并调用了相应的函数,来输出理想和实际的粮食产量。 首先,由于 Crop_assessment 定义的成员数据缺省为私有的,所以想要获得实际和理想的粮食产量,要通过两个公有成员函数,所以(1)处应填入“return actual_crop”,(2)处应填入“return ideal_crop”。 其次,在类 Lot_size 中,由于我们要反映矩形域上的粮食产量,所以我们在类中嵌套定义了一个类,(3)应填入“Crop_assessment”。 最后,由于我们想通过 Lot_size 类中的getdata 和 get_data2 函数得到粮食产量,但由于这

35、两个成员数据是私有函数,所以我们必须通过定义的对象调用它,所以(4)应填入“crop.get ideal_crop()”,(5)应填入“crop.get actual_crop()”。六、B试题六/B(总题数:1,分数:3.00)6.阅读以下说明和 Java 程序,将应填入U (n) /U处的字句写在对应栏内。 说明 下面程序输出一个矩形面积,以及矩形区域上的假想的作物产量。 Java 程序 public class MainJava public static void main(String args) Lot_size small=new Lot_size(); Lot_size med

36、ium=new Lot_size(); small.set(5,5,5,25); medium.set(10,10,10,50); System.out.println(“For a small lot of area“ +small.get_area()+“/n“); System.out.println(“the actual crops are $“ +small.get_data2()+“/n“); System.out.println(“and ideal crops are $“ +small.get data()+“/n“); System.out.println(“For a

37、medium lot of area“ +medium.get_area()+“/n”); System.out.println(“the actual crops are $“ +medium.get_data2()+“/n“); System.out.println (“and ideal crops are $“ +medium.get_data()+“/n“); class Crop_assessment private int actual_crop; private int ideal_crop; public void set(int in_actual,int in ideal

38、) actual_crop=in_actual; ideal_crop=in_ideal; public int get_actual_crop()return U(1) /U; public int get_ideal_crop()(return U(2) /U;class Lot_size private int length; private int width; private Crop_assessment crop=U (3) /U; public void set(int 1,int W,int a,int i) length=1; width=W; crop.set(a,i);

39、 public int get_area()return length*width; public int get_data()freturnU (4) /U; public int get_data2()(returnU (5) /U;(分数:3.00)填空项 1:_ (正确答案:return actual_crop (2) return ideal_crop (3) new Crop_assessment())解析:(4) crop.get_ideal_crop (5) crop.get_actual_crop() 本题以 Java 语言为载体,考查面向对象程序设计中的几个重要概念类的嵌套

40、及函数的使用。 本题的功能是通过已定义的粮食收成类定义了一个计算特定矩形域上粮食收成的类,在主函数中定义了两个对象,并调用了相应的函数,来输出理想和实际的粮食产量。 首先,由于 Crop assessment 定义的成员数据缺省为私有的,所以想要获得实际和理想的粮食产量,要通过两个公有成员函数,所以(1)处应填入“return actual_crop”,(2)处应填入“return ideal_crop”。 其次,在类 Lot size 中,由于我们要反映矩形域上的粮食产量,所以我们在类中嵌套定义了一个类,Java 中对象需要实例化,故(3)应填入“new Crop_assessment()”。 最后,由于我们想通过 Lot_size 类中的 get_data 和 get_data2 函数得到粮食产量,但由于这两个成员数据是私有函数,所以我们必须通过定义的对象调用它,所以(4)应填入“crop.get_ideal_crop()”,(5)应填入“crop.get_actual_crop()”。

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 考试资料 > 职业资格

copyright@ 2008-2019 麦多课文库(www.mydoc123.com)网站版权所有
备案/许可证编号:苏ICP备17064731号-1