试题四
阅读下列函数说明和C函数,将应填入 (n) 处的字句写在答题纸的对应栏内。
[函数说明]
函数DeleteNode(Bitree *r,int e)的功能是:在树根结点指针为r的二叉查找(排序)树上删除键值为e的结点,若删除成功,则函数返回0,否则函数返回-1。二叉查找树结点的类型定义为:
typedef struct Tnode{
int data;
struct Tnode *Lchild,*Rchild;
}*Bitree;
在二叉查找树上删除一个结点时,要考虑三种情况:
1若待删除的结点p是叶子结点,则直接删除该结点;
2若待删除的结点p只有一个子结点,则将这个子结点与待删除结点的父结点直接连接,然后删除结点p;
3若待删除的结点p有两个子结点,则在其左子树上,用中序遍历寻找关键值最大的结点s,用结点s的值代替结点p的值,然后删除结点s,结点s必属于上述1、2情况之一。
[函数代码]
int DeleteNode(Bitree *r,int e) {
Bitreep = *r, pp, s, c;
while( (1) ) { /*从树根结点出发查找键值为e的结点*/
pp = p;
if ( e< p->data) p = p -> Lchild;
else p = p->Rchild;
}
if(!p) return –1; /* 查找失败 */
if(p->Lchild && p->Rchild) { /* 处理情况3 */
s = (2);pp = p;
while ( (3) ) { pp = s; s = s-> Rchild; }
p->data = s ->data; p = s;
}
/*处理情况1、2*/
if( (4) ) c = p -> Lchild;
elsec = p -> Rchild;
if(p == *r) *r = c;
elseif ( (5) ) pp -> Lchild = c;
elsepp->Rchild = c;
free(p);
return 0;
}
第1题:
阅读下列函数说明和C代码,将应填入 处的字句写在答题纸的对应栏内。
[函数1.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是“完全数”。
[函数1.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;
return -1;
}
[函数1.2说明]
函数int maxint(int a[], int k)的功能是用递归方法求指定数组中前k个元素的最大值,并作为函数值返回。
[函数1.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;
第2题:
●试题二
阅读下列函数说明和C代码,将应填入(n)处的字句写在答题纸的对应栏内。
【说明】
该程序运行后,输出下面的数字金字塔
【程序】
include<stdio.h>
main ()
{char max,next;
int i;
for(max=′1′;max<=′9′;max++)
{for(i=1;i<=20- (1) ;++i)
printf(" ");
for(next= (2) ;next<= (3) ;next++)
printf("%c",next);
for(next= (4) ;next>= (5) ;next--)
printf("%c",next);
printf("\n");
}
}
第3题:
()阅读下列说明和C语言程序,将应填入 (n)处的语句写在答题纸的对应栏内。[说明]下面程序是一个带参数的主函数,其功能是显示在命令行中输入的文本文件内容。[C语言函数]#include"stdio.h"main(argc,argv) int argc; char *argv[]; { (1) ; if((fp=fopen(argv[1],”r’’))== (2) ) { printf(”file not open!\n”);exit(0);} while( (3) ) putchar( (4) ); (5); }
第4题:
阅读以下函数说明和C语言函数,将应填入(n)处的字句写在答题纸的对应栏内。
【函数2.1说明】
递归函数sum(int a[], int n)的返回值是数组a[]的前n个元素之和。
【函数2.1】
int sum (int a[],int n)
{
if(n>0) return (1);
else (2);
}
【函数2.2说明】
有3个整数,设计函数compare(int a,int b,int c)求其中最大的数。
【函数2.2】
int compare (int a, int b, int c )
{ int temp, max;
(3) a:b;
(4) temp:c;
}
【函数2.3说明】
递归函数dec(int a[],int n)判断数组a[]的前n个元素是否是不递增的。不递增返回 1,否则返回0。
【函数2.3】
int dec( int a[], int n )
{
if(n<=1) return 1;
if(a[0]<a[1]) return 0;
return (5);
}
第5题:
试题三(共 15 分)
阅读以下说明和 C 程序,将应填入 (n) 处的字句写在答题纸的对应栏内。
第6题: