第三章和第四章随堂测验考试

第三章和第四章随堂测验考试

判断题

第三章和第四章随堂测验考试

单选题

第三章和第四章随堂测验考试

填空题

第三章和第四章随堂测验考试

程序填空题

第三章和第四章随堂测验考试

函数题

6-1 另类循环队列 (20 分)
如果用一个循环数组表示队列,并且只设队列头指针Front,不设尾指针Rear,而是另设Count记录队列中元素个数。请编写算法实现队列的入队和出队操作。

函数接口定义:
bool AddQ( Queue Q, ElementType X );
ElementType DeleteQ( Queue Q );
其中Queue结构定义如下:

typedef int Position;
typedef struct QNode *PtrToQNode;
struct QNode {
ElementType Data; / 存储元素的数组 /
Position Front; /
队列的头指针 /
int Count; /
队列中元素个数 /
int MaxSize; /
队列最大容量 */
};
typedef PtrToQNode Queue;
注意:如果队列已满,AddQ函数必须输出“Queue Full”并且返回false;如果队列是空的,则DeleteQ函数必须输出“Queue Empty”,并且返回ERROR。

裁判测试程序样例:
#include <stdio.h>
#include <stdlib.h>

#define ERROR -1
typedef int ElementType;
typedef enum { addq, delq, end } Operation;
typedef enum { false, true } bool;
typedef int Position;
typedef struct QNode *PtrToQNode;
struct QNode {
ElementType Data; / 存储元素的数组 /
Position Front; /
队列的头、尾指针 /
int Count; /
队列中元素个数 /
int MaxSize; /
队列最大容量 */
};
typedef PtrToQNode Queue;

Queue CreateQueue( int MaxSize )
{
Queue Q = (Queue)malloc(sizeof(struct QNode));
Q->Data = (ElementType *)malloc(MaxSize * sizeof(ElementType));
Q->Front = 0;
Q->Count = 0;
Q->MaxSize = MaxSize;
return Q;
}

bool AddQ( Queue Q, ElementType X );
ElementType DeleteQ( Queue Q );

Operation GetOp(); /* 裁判实现,细节不表 */

int main()
{
ElementType X;
Queue Q;
int N, done = 0;

scanf("%d", &N);
Q = CreateQueue(N);
while ( !done ) {
    switch( GetOp() ) {
    case addq: 
        scanf("%d", &X);
        AddQ(Q, X);
        break;
    case delq:
        X = DeleteQ(Q);
        if ( X!=ERROR ) printf("%d is out\n", X);
        break;
    case end:
        while (Q->Count) printf("%d ", DeleteQ(Q));
        done = 1;
        break;
    }
}
return 0;

}

/* 你的代码将被嵌在这里 */
输入样例:
4
Del
Add 5
Add 4
Add 3
Del
Del
Add 2
Add 1
Add 0
Add 10
End
输出样例:
Queue Empty
5 is out
4 is out
Queue Full
3 2 1 0

代码如下

bool AddQ( Queue Q, ElementType X ){
if(Q->Count == Q->MaxSize){
        printf("Queue Full\n");
    return false;
}
Q->Count++;
Q->Data[(Q->Front+Q->Count)%Q->MaxSize] = X;
return true;
}
ElementType DeleteQ( Queue Q ){
if(Q->Count == 0){
    printf("Queue Empty\n");
    return ERROR;
}
Q->Count--;
Q->Front = (Q->Front+1)%Q->MaxSize;
return Q->Data[Q->Front];
}

6-2 字符串 - 6. 查找子串(BF算法)* (20 分)
C语言标准函数库中包括 strstr 函数,在主串中查找子串。作为练习,我们自己编写一个功能与之相同的函数。

函数原型
// 查找子串
char* StrStr(const char *txt, const char *pat);
说明:txt 和 pat 分别为主串和子串的起始地址。若查找成功,则函数值为子串在主串中首次出现的起始地址,否则函数值为NULL。

特别地,我们对C语言库函数strstr进行适当修改:若子串为空串,则没有意义,函数值规定为NULL。

裁判程序
#include <stdio.h>

// 查找子串
char* StrStr(const char *txt, const char *pat);

int main()
{
char m[1024], s[1024], *p;
gets(m);
gets(s);
p = StrStr(m, s);
if §
{
printf("%d\n", p - m);
}
else
{
puts(“Null”);
}
return 0;
}

/* 你提交的代码将被嵌在这里 */
输入样例1
This is a pencil
is
输出样例1
2
输入样例2
This is a pencil
be
输出样例2
Null

代码如下

char* StrStr(const char *txt, const char *pat){
    char *t=(char *)txt;
    char *p =(char *)pat;
    int len1= 0;
    int len2 = 0;
while(*txt++)
len1++;
while(*pat++)
    len2++;
    if(len2 <=0 )
        return NULL;
    int i = 0,j=0;
while(i < len1 && j <len2){
    if(t[i] == p[j])
    {
        ++i;
        ++j;
    }
    else{
        j =0;
        i = i - j + 1;
    }
}
if(j >= len2)
    return t+(i-len2);
    return NULL;
}