●试题一
阅读下列算法说明和算法,将应填入(n)处的语句写在答题纸的对应栏内。
【说明】
为了减少直接插入排序关键字的比较次数,本算法使用了二分(折半)插入法对一个无序数组R[1..n]进行排序。排序思想是对一个待插入元素,先通过二分法(折半)找到插入位置,后移元素后将该元素插入到恰当位置(假设R[]中的元素互不相同)。
【算法】
1.变量声明
X:DataType
i,j,low,high,mid,R0..n
2.每循环一次插入一个R[i]
循环:i以1为步长,从2到n,反复执行
①准备
X<-R[i]; (1) ;high<-i-1;
②找插入位置
循环:当 (2) 时,反复执行
(3)
若X.key<R[mid].key
则high<-mid-1
否则 (4)
③后移
循环:j以-1为步长,从 (5) ,反复执行
R[j+1]<-R[j]
④插入
R[low]<-X
3.算法结束
查看答案
●试题四
阅读以下说明和C代码,将应填入(n)处的字句写在答题纸的对应栏内。
【说明】
从文件IN.DAT中读取一篇英文文章存入到字符串数组XX中;请编写程序,其功能是:以行为单位把字符串中所有小写字母o左边的字符串内容移到该串的右边存放,然后把小写字母o删除,余下的字符串内容移到已处理字符串的左边存放。最后把已处理的字符串仍按行重新存入字符串数组XX中,最后调用函数WRITEDAT(),把结果XX输出到文件OUT5.DAT中。
例如:原文:You can create an index on any field.
you have the correct record.
结果:n any field.Yu can create an index
rd.yu have the crrect rec
原始数据文件存放的格式是:每行的宽度均小于80个字符,含标点符号和空格。
【函数】
#include "stdio.h"
#include "string.h"
#include "conio.h"
#include "ctype.h"
#include "mem.h"
unsigned char xx[50][80];
int maxline=0;
int readdat(void);
void writedat(void);
/*将题目要求的字符串中所有小写字母o左边的字符串内容移到该串的右边存放,即
将串中"最后"一个字母o左右两侧的内容互换*/
void StrOR(void)
{
inti;
char*p1,*p2,t[80];
for(i=0;i<maxline;i++)
{ t[0]=′/0′;
p2=xx[i];
while(*p2)/*找到最后一个′o′*/
{if((1) )p1=p2;
p2++;
}
strcat(t,p1+1);
*p1=′\\0′;
strcat(t,xx[i]);
p1=xx[i];
p2=t;
while(*p2)/*删去字符′o′*/
{if((2) ) (3) =*p2;
p2++;
}
(4) ;
}
}
void main()
{
clrscr();
if(readdat())
{printf("Can't open the file IN.DAT!\\n");
return;
}
StrOR();
writedat();
}
int readdat(void)
{
FILE*fp;
int i=0;
char*p;
if((fp=fopen("in.dat","r"))==NULL)
return 1;
while(fgets(xx[i],80,fp)!=NULL)
{p=strchr(xx[i],′\\n′);
if(p)
*p=0;
i++;
}
maxline= (5) ;
fclose(fp);
return 0;
}
void writedat(void)
{FILE*fp;
int i;
fp=fopen("in.dat","w");
for(i=0;i<maxline;i++)
{printf("%s\n",xx[i]);
fprintf(fp,"%s\n",xx[i]);
}
fclose(fp);
}
}
}
●试题七
阅读以下说明和Java代码,将解答写入答题纸的对应栏内。
【说明】
下面程序的功能是找出所有三位数中,个、十、百位数字的立方和等于该数本身的三位数并显示。在程序的每条横线处填写一个适当的词或语句,使程序的功能完整。
public (1) class cube_Root{
public static void (2) main(String args[]){
System.out.println("个、十、百位数字的立方和等于该数本身的三位数有:");
for(int n=100;n<1000;n++)
{
int a,b,c;
(3)
b=n/10%10;
(4)
if((5) )
System.out.println(n);
}
}
}
(15)程序段:int x=12; double y=3.141593; printf(“%d%8.6f”,x,y);的输出结果是
A)123.141593 B)12 3.141593 C)12,3.141593 D)123.141593
(33)有以下程序
#include <stdio.h>
int fun{int x,int y}
{ if(x==y) return(x);
else return((x+y)/2)
}
main()
{ int a=4,b=5,c=6;
printf(“%d\n”,fun(2*a,fun(b,c)))
}
程序运行后的输出结果是
A)3 B)6 C)8 D)12