问答题【说明】
在下面程序横线处填上适当的字句,3个(2)空填的是一样的。使其输出结果为:
x=5
x=6
y=7
x=8
z=9
【程序】
#include<iostream.h>
class X1{
int x;
{{U}} (1) {{/U}};
X1(int xx=0){x=xx;}
{{U}} (2) {{/U}}void Output(){
cout<<"x="<<x<<end;
}
};
{{U}} (3) {{/U}}Y1:public X1{
int y;
public:
Y1(int xx=0,int yy=0);X1(xx){y=yy;}
{{U}} (2) {{/U}}void Output(){
{{U}} (4) {{/U}}Output();
cout<<"y="<<y<<endl;
}
};
class Z1:public X1{
int z:
{{U}} (5) {{/U}};
Z1(int xx=0,int zz=0):X1(xx)(z=zz;}
{{U}} (2) {{/U}}void Output(){
X1∷Output();
cout<<"z="<<z<<endl;
}
};
void main()
{
X1 a(5);Y1 b(6,7);Z1 c(8,9);
X1*p[3]={
For(int i=0;i<3;i++){
p[i]->Output();cout<<endl;
}
}
问答题【说明】
下面程序完成从键盘读入一个字符串,然后采用parseInt方法将其转换为一个相应的整数。
import java.io.*;
public class testThrows{
public static{{U}} (1) {{/U}}readString(){{U}} (2) {{/U}}IOException{
int ch;
String r="";
boolean done=false;
while({{U}} (3) {{/U}}){
ch=System.in.read();
if(ch<0 || ch=0xd) //处理回车符中第一个符号
done=true;
else
r=r+(char)ch;
}
return r;
}
public static void main(Stling args[]){
String str;
{{U}} (4) {{/U}}{
str=readString();
}{{U}} (5) {{/U}}(IOException e){
System.out.println("error");
return;
}
System.out.println("input integer: "+Integer.parselnt(str));
}
}
问答题试题二
阅读下列函数说明和C 函数,将应填入 (n) 处的字句写在答题纸的对应栏内。
[函数2.1 说明]
函数palindrome(char s[])的功能是:判断字符串s 是否为回文字符串,若是,则返回0,否则返回-1。若一个字符串顺读和倒读都一样时,称该字符串是回文字符串,例如:“LEVEL”是回文字符串,而“LEVAL”不是。
[函数2.1]
int palindrome(char s[])
{ char *pi, *pj;
pi = s; pj = s+strlen(s)-1;
while ( pi < pj pj--;
}
if ( (2) ) return -1;
else return 0;
}
[函数2.2 说明]
函数f(char *str,char del)的功能是:将非空字符串str 分割成若干个子字符串并输出,del 表示分割时的标志字符。
例如若str 的值为“33123333435”,del 的值为‘3’,调用此函数后,将输出三个子字符串,分别为“12”、“4”和“5”。
[函数2.2]
void f(char *str,char del)
{ int i, j, len;
len = strlen(str);
i = 0;
while( i < len ){
while( (3) ) i++; /*忽略连续的标志字符*/
/*寻找从str[i]开始直到标志字符出现的一个子字符串*/
j = i + 1;
while(str[j] != del
4) = ′/0′; /*给找到的字符序列置字符串结束标志*/
printf("%s/t",
5) ;
}
}
问答题【说明】
下面的程序是从命令行输入3个数传递到public static void main(String args[])方法中 (如java IsTriangle 3 4 5),并判断这3个数能否构成三角形的3条边,并显示相应的结果。请在程序的每条横线处填入适当的语句,使程序的功能完整。
注意:请勿改动main()主方法和其他已有的语句内容,仅在横线处填入适当的语句。
public class IsTriangle{
public static void main( String args[ ]){
int a[] =new {{U}}(1) {{/U}}[args. {{U}}(2) {{/U}}];
for(int i=0;i<3; {{U}}(3) {{/U}})
{
a[i]= {{U}}(4) {{/U}}
}
if({{U}} (5) {{/U}})
System. out. println(a[0] +","+a[1] +","+a[2]"能构成三角形的3条边");
else
System. out. println(a[0] +","+a[1] +","+a[2]"不能构成三角形的3条边);
}
}
问答题阅读以下说明和C程序,填充函数中的空缺,将解答填入答题纸的对应栏内。【说明】N个游戏者围成一圈,从1~N顺序编号,游戏方式如下;从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,直到剩余一个游戏者为止,该游戏者即为获胜者。下面的函数playing(Linklisthead)模拟上述游戏过程并返回获胜者的编号。其中,N个人同成的圈用一个包含N个结点的单循环链表来表示,如图4—1所示,游戏者的编号放在结点的数据域中。在函数中,以删除结点来模拟游戏者退出圈子的处理。整型变量c(初值为1)用于计数,指针变量P的初始值为head,如图4一1所示。游戏时,从P所指向的结点开始计数,P沿链表中的指针方向遍历结点,c的值随P的移动相应地递增。当c计数到2时,就删除P所指结点的下一个结点(因下一个结点就表示报数到3的游戏者),如图4—2所示,然后将C设置为0后继续游戏过程。结点类型定义如下:typedefstructnode{intcode;/*游戏者的编号*/structnode*next;}NODE,*LinkList;【C函数】intplaying(LinkListhead,intn){/*head指向含有n个结点的循环单链表的第一个结点(即编号为1的游戏者)*/LinkListp=head,q;intthewinner,c=1;while(n>____(1)____){if(c==2){/*当c等于2时,p所指向结点的后继即为将被删除的结点*/q=p->next;p->next=____(2)____;printf(“%d\t”,q->code);/*输出退出圈子的游戏者编号*/free(q);c=____(3)____;n--;}/*if*/p=____(4)____;c++:}*while*/theWinner=____(5)____;free(p);returntheWinner;/*返回最后一个游戏者(即获胜者)的编号*/}
问答题阅读以下说明和C函数代码,回答问题并将解答写在对应栏内。【说明】著名的菲波那契数列定义式为f1=1f2=1fn=fn-1+fn-2(n=3,4,…)因此,从第1项开始的该数列为1,1,2,3,5,8,13,21,…。函数fibl和fib2分别用递归方式和迭代方式求解菲波那契数列的第n项(调用fib1、fib2时可确保参数n获得一个正整数)。【C函数代码】
问答题试题五(共15 分)
阅读以下说明和C 语言函数,将应填入 (n) 处的字句写在答题纸的对应栏内。
[说明]
二叉排序树或者是一棵空树,或者是具有如下性质的二叉树:若它的左子树非空,则左子树上所有结点的值均小于根结点的值;若它的右子树非空,则右子树上所有结点的值均大于根结点的值;左、右子树本身就是两棵二叉排序树。
函数insert_BST (char *str)的功能是:对给定的字符序列按照ASCII 码值大小关系创建二叉排序树,并返回指向树根结点的指针。序列中重复出现的字符只建一个结点,并由结点中的Count 域对字符的重复次数进行计数。
二叉排序树的链表结点类型定义如下:
typedef struct BSTNode{
char Elem; /*结点的字符数据*/
int Count; /*记录当前字符在序列中重复出现的次数*/
struct BSTNode *Lch,*Rch; /*结点的左、右子树指针*/
}*BiTree;
[函数]
BiTree insert_BST(char *str)
{ BiTree root,parent,p;
char (1) ; /* 变量定义及初始化 */
root = (BiTree)malloc(sizeof(struct BSTNode));
if (!root || *s=='/0') return NULL;
root->Lch = root->Rch = NULL; root->Count = 1; root->Elem = *s++;
for(; *s != '/0'; s++) {
(2) ; parent = NULL;
while (p) { /* p 从树根结点出发查找当前字符*s 所在结点 */
parent = p;
if (*s == p->Elem) /*若树中已存在当前字符结点,则当前字符的计数值加1*/
{ p->Count++; break; }
else /*否则根据字符*s 与结点*p 中字符的关系,进入*p 的左子树或右子树*/
if (*s > p->Elem) p = p->Rch;
else p = p->Lch;
}/*while*/
if ( (3) ) { /* 若树中不存在字符值为*s 的结点,则申请结点并插入树中 */
p = (BiTree)malloc(sizeof(struct BSTNode));
if (!p) return NULL;
p->Lch = p->Rch = NULL; p->Count = 1; p->Elem = *s;
/*根据当前字符与其父结点字符值的大小关系,将新结点作为左子树或右子树插入*/
if (p->Elem > parent->Elem ) (4) = p;
else (5) = p;
}
}/*for*/
return root;
}
问答题[说明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=1,s=0;i<=n/2;i++) if(n%i=0) (1) ; if( (2) )return 0; rerurn-1; [说明2] 函数int maxint(int a[],int k)的功能是用递归方法求指定数组中前k个元素的最大值,并作为函数值返回。 [C函数2] int maxint(int a[],int k) int t; if( (3) ) return (4) ; t=maxint(a+1, (5) ); return(a[0]>t)?a[0]:t;
问答题【Java代码】
class usethread implements{{U}} (1) {{/U}}{
int num
usethread(int n){
num=n;
}
public void{{U}} (2) {{/U}}{
for(int i=0;i<3;i++)
System.out.println("running:"+num);
System.out.println("finished:"+num);
}
public class multhread{
public static void main(String args[]){{U}} (3) {{/U}}InterruptedException{
Thread m1=new Thread(new usethread(1));
Thread m2=new Thread(new usethread(2));
m1.start();
m2.start();
m1.join();
m2.join();
}
}
【问题1】
补充完整上面Java代码中{{U}} (n) {{/U}}处。
【问题2】
写出上面Java代码运行的结果。
问答题【说明】 字符串在程序设计中扮演着重要角色。现需要设计字符串基类string,包含设置字 符串、返回字符串长度及内容等功能。另有一个具有编辑功能的串类edlt_string,派生于string,在其中设置一个光标,使其能支持在光标处的插入、删除操作。 【程序】 #include <iostream.h> #include <stdio.h> #include <string.h> class string int length; char *data; public: int get_length() return length; char *get_data() return data; ~string() delete data; int set data(int in_length, char *in_data); int set_data(char *data); void print() cout<<data<<endl; ; class edit_string: public string int cursor; public: int get_cursor() return cursor; void move_cursor(int dis) cursor=dis; int add_data(string *new_data); void delete_data(int num); ; int string::set_data(int in_length,char *in_data) length=in_length; if(!data) delete data; (1) strcpy(data,in_data); return length; int string::set data(char *in_data) (2) if(!data) delete data; (1) strcpy(data,in_data); return length; int edit_string::add_data(string *new_data) int n,k,m; char *cp,*pt; n=new_data->get_length(); pt=new_data->get_data(); cp=this->get_data(); m=this->get_length(); char *news=new char[n+m+1]; for(int i=0; i<cursor; i++) news[i]=cp[i]; k=i; for(int j=0; j<n; i++,j++) news[i]=pt[j]; cursor=i; for(j=k; j<m; j++,i++) (3) news[i]='/0'; (4) delete news; return cursor; void edit string::delete_data( int num) int m; char *cp; cp=this->get_data(); m=this->get_length(); for(int i=cursor; i<m; i++) (5) cp[i]='/0';
问答题【说明】
有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数。
【函数】
main()
{
int number[20],n,m,i;
printf("the total numbers is:");
scanf("%d",
printf("back m:");
scanf("%d",
for(i=0;i<=n-1;i)
scanf("%d,",
{{U}} (1) {{/U}};
for(i=0;i<=n-1;i)
printf("%d,",number[i]);
}
move(array,n,m)
int n,m,array[20];
{
int *p,array_end;
array_end={{U}} (2) {{/U}};
for(p=array[n-1];{{U}} (3) {{/U}};p--)
*p={{U}} (4) {{/U}};
*array=array_end;
m--;
if(m>0){{U}} (5) {{/U}};
}
问答题【说明】
设计一个类模板SamPle用于对一个有序数组采用二分法查找元素下标。
【C++程序】
#include < iostream. h >
#define Max 100 //最多元素个数
template < class T >
class Sample
{
T A[Max]: //存放有序数序
int n: //实际元素个数
public
Sample( ) { } //默认构造函数
Sample(T a[] ,int i); //初始化构造函数
int seek(T c);
void disp( )
{
for(int i=0;i <n;i ++)
cout<<A[i] <<" ";
cout<<endl:
} } template < class T >
Sample <T>: :Sample(T a[ ],int i)
{
n=i:
for( intj =0;j < i;j ++ )
{{U}} (1) {{/U}};
}
template < class T >
int Sample < T >:: seek( T c)
{
int low =0,high = n-1 ,mid;
while({{U}} (2) {{/U}})
{
mid = (low + high)/2;
if({{U}} (3) {{/U}})
return mid;
else if({{U}} (4) {{/U}})
low=mid+|;
else
{{U}} (5) {{/U}};
}
return-1;
}
void main( )
{
char a[ ] ="acegkmpwxz";
Sample < char > s(a, 1);
cout<<"元素序列:" ;s. disp( );
cout<<"元素'g'的下标:"<<s. seek('g') <<endl;
}
问答题【说明】
计算n的合数。一个整数n可以有多种划分,使其划分的一列整数之和为n。例如,整数5的划分为:
5
4 1
3 2
3 1 1
2 2 1
2 1 1 1
1 1 1 1 1
共有7种划分。这种划分的程序如下所示。
【程序】
#include <stdio.h>
int n[1000],m,k;
void output sum()
{
int j;
for(j=0;n[j]!=0;j++)
printf("%d/t",n[j]);
printf("/n");
}
void sum(int i)
if(m-n[i]<n[i])
{ m=m-n[i];
{{U}}(1) {{/U}}
i++;
n[i+1]=0;
}
else
{
{{U}} (2) {{/U}}
m-=n[i];
i++;
}
if(m!=n[i])
sum(i);
else
output_sum();
if(n[i]>1)
{
n[i]--;
{{U}}(3) {{/U}}
}
else
{
while((n[i]==1)
{{U}} (4) {{/U}}
}
if(i!=0)
{
{{U}} (5) {{/U}}
sum(i);
}
}
}
void main()
{
int i;
scanf("%d",
m=k=n[0];
for(i=1;i<=k;i++)
n[i]=0;
while(n[0]!=1)
{
n[0]--;
i=0;
sum(0);
m=k;
}
}
问答题【说明】 java.util库中提供了Vector模板类,可作为动态数组使用,并可容纳任意数据类型。该类的部分方法说明如下表所示:
方法名
含 义
add(k)
向vector对象的尾部添加一个元素k
removeElementAt(i)
删除序号为i的元素(vector元素序号从0开始)
isEmpty()
判断vector对象是否含有元素
size()
返回vector对象中所包含的元素个数【Java代码】 import {{U}}(1) {{/U}}; public class JavaMain { static private final int {{U}}(2) {{/U}} = 6; public static void main(String[] args){ Vector<Integer> theVector = new Vector<{{U}} (3) {{/U}}>(); // 初始化 theVector, 将theVector的元素设置为0至5 for (int cEachItem = 0; cEachItem < ARRAY_SIZE; cEachItem++) theVector.add({{U}} (4) {{/U}}); showVector(theVector); // 依次输出theVector中的元素 theVector.removeElementAt(3); showVector(theVector); } public static void showVector(Vector<Integer> theVector if (theVector.isEmpty()) { System.out.println("theVectcr is empty."); return; } for (int loop = 0; loop < theVector.size(); loop++) System.out.print(theVector.get(loop)); System.out.print(", "); } System.out.println(); } } 该程序运行后的输出结果为: 0,1,2,3,4,5 {{U}} (5) {{/U}}
问答题阅读以下应用说明、图和C++代码,根据要求回答问题1至问题3。
[说明] 已知以下C++程序运行时的输出结果如下。
1:1 1:1 1:1
[C++程序] 01 #include <iostream> 02
using namespace std; 03 class Stock{
04 protect: 05 {{U}} (1)
{{/U}}{ }; 06 Stock(int n, int pr=1)
{ 07 shares = n;
share_val=pr; 08 }; 09
void ~Stock() { }; 10
public: 11 //成员函数 12
void output() { 13
{{U}} (2) {{/U}} << shares << ":" << share_val <<
endl; 14 } 15
private: 16 //成员变量 17
int shares; 18 int
share_val; 19 }; 20 21
void main() { 22 Stock a(1);
a.output 23
Stock b;
b.output 24 Stock c = Stock ();
c.output 25 }
问答题【说明】下列流程图用泰勒(Taylor)展开式y=ex=1+x+x2/2!+x3/3!+…+xn/n!+…计算并打印ex的近似值,其中用ε(>0)表示误差要求。【流程图】
问答题【说明】下列流程图用泰勒(Taylor)展开式y=ex=1+x+x2/2!+x3/3!+…+xn/n!+…计算并打印ex的近似值,其中用ε(>0)表示误差要求。【流程图】
问答题【说明】 下面的程序构造一棵以二叉链表为存储结构的二叉树。 【函数】 BitTree *createbt(BitTree *bt) BitTree *q; struct node *s[30]; int j,i; char x; printf("i,x="); scant("%d,%c", while(i!=0 //生成一个结点 (1) ; q->lchild=NULL; q->rchild=NULL; (2) ; if ( (3) ) j=i/2; // j为i的双亲结点 if(i%2==0) (4) ; //i为j的左孩子 else (5) ; //i为j的右孩子 printf("i,x="); scanf("%d,%c", return s[i];
问答题[函数2.1说明]Fibonacci数列中头两个数均为1,从第三个数开始,每个数等于前两个数之和。下述程序计算Fibonacci数列中前15个数,并以每行5个数的格式输出。[函数2.1]#include<stdio.h>main(){inti,f,f1=1,f2=1;printf("%5d%5d",f1,f2);for(i=3;i<=15;i++){f={{U}}(1){{/U}};printf("%5d",f);if({{U}}(2){{/U}}==0)printf("/n");f1=12;{{U}}(3){{/U}};}}[试题一流程图][函数2.2说明]函数fun(char*str1,char*str2)的功能是将字符串str2拼接到str1之后。[函数2.2]fun(char*str1,char*str2){inti,j;for(i=0;str1[i]!='/0';i++);for(j=0;str2[j]!='/0';j++){{U}}(4){{/U}};{{U}}(5){{/U}};}
问答题【程序2.1说明】 已知一个排好序的数组,现输入一个数,要求按原来的顺序规律,将它插入到数组中。 【程序2.1】 #include <stdioh> #define N 100 void main() float a[N+l],x; int i,p; printf("输入已经排好序的数列: "); for(i=0; i<N; i++) scanf(%f", printf("输入要插入的数:"); scanf("%f", for(i=0,p=N; i<N; i++) if(x<a[i]) (1) break; for(i=N-1; i>=p; i--) (2) (3) for(i=0; i<=N; i++) prinff("%f/t",a[i]); 【程序2.2说明】 本程序用变量count统计文件中字符的个数。 【程序2.2】 #include <stdio.h> #include <stdlib.h> void main() FILE *fp; long count=0; if((fp=fopen("letter.txt","r"))==NULL) printf("can not open file/n"); exit(0); while(!feof(fp)) (4) count++; printf("count=%d/n",count); (5)