B. 春日填空

题目类型:答案提交 评测方式:Special Judge

题目描述

【填空题作答说明】

你应该使用尽可能简洁,且最短的答案进行填空,例如,你应该使用a==m-1而不是a-2==m-3,使用a++而非a+=1,尽管它们都是合理的。所有的待填内容均由下划线标识,一道填空题可能有多个空,请用一个空格将答案隔开,同时不要填写无意义的空格,这可能导致你的全部答案被判零分。空题请在原本的位置上任意填写一个字符。

请直接把答案填写在右侧窗口并提交

1.请完成以下代码,实现文件的复制(从源文件src复制到目标文件dst)。

void copy_file(const char *src, const char *dst) {
    FILE *src_file = fopen(src, "______"); // 请填写一个字符,下行同
    FILE *dst_file = fopen(dst, "______");

    if (!src_file || !dst_file) {
        printf("Error opening files.\n");
        return;
    }

    int c;
    while ((c = fgetc(______)) != EOF) {
        fputc(______);
    }

    fclose(src_file);
    fclose(dst_file);
}

2.请完成以下代码,实现结构体数组中最高分学生的查找,并输出学生的姓名和分数。

#include <stdio.h>

struct student {
    int id;
    char name[50];
    float score;
};

int main() {
    struct student students[] = {
        {1, "Alice", 87.5},
        {2, "Bob", 91.0},
        {3, "Cathy", 78.5},
        {4, "David", 92.5},
    };

    int max_index = 0;
    for (int i = 1; i < sizeof(students) / sizeof(students[0]); i++) {
        if (students[i].score > ______) {
            max_index = i;
        }
    }

    printf("The highest score student is %s, with a score of %.1f.\n", students[max_index].name, students[max_index].score);
    return 0;
}

3.请完成以下代码,实现简单的栈结构,包括push和pop操作,并在main函数中测试。

#include <stdio.h>
#include <stdbool.h>

#define MAX_SIZE 1000000000000000000

int stack[MAX_SIZE];
int top = -1;

bool is_full() {
    return top == ______;
}

bool is_empty() {
    return top == -1;
}

void push(int value) {
    if (!is_full()) {
        ______;
        stack[top] = value;
    } else {
        printf("Stack is full.\n");
    }
}

int pop() {
    if (!is_empty()) {
        return ______;
    } else {
        printf("Stack is empty.\n");
        return -1;
    }
}

int main() {
    push(1);
    push(2);
    push(3);
    printf("Pop: %d\n", pop());
    printf("Pop: %d\n", pop());
    return 0;
}

4.请给出下述程序的执行结果。

#include <stdio.h>
int popcount(int x) {
    int cnt = 0;
    while (x)
        cnt++, x &= x - 1;
    return cnt;
}
int main() {
    int x = 0x12345678;
    int y = 0x7fffffff;
    int z = 0x43654290;
    printf("%d,%d,%d", popcount(x), popcount(y), popcount(z));
    return 0;
}

5.请完成以下代码,实现一个简单的动态数组结构,包括插入元素操作,并在main函数中测试。

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

typedef struct {
    int *data;
    int size;
    int capacity;
} dynamic_array;

dynamic_array *create_array(int capacity) {
    dynamic_array *array = (dynamic_array *)malloc(sizeof(dynamic_array));
    array->data = (int *)malloc(capacity * sizeof(int));
    array->size = 0;
    array->capacity = capacity;
    return array;
}

void insert_element(dynamic_array *array, int value) {
    if (array->size == array->capacity) {
        array->capacity *= 2;
        array->data = (int *)realloc(array->data, array->capacity * sizeof(int)); // 划线处为类型转换
    }
    array->data[array->size] = ______;
    ______;
}

int main() {
    dynamic_array *array = create_array(2);
    insert_element(array, 1);
    insert_element(array, 2);
    insert_element(array, 3);
    printf("Array[0]: %d\n", array->data[0]);
    printf("Array[1]: %d\n", array->data[1]);
    printf("Array[2]: %d\n", array->data[2]);
    return 0;
}

6.请完成以下代码,实现一个简单的循环链表结构,包括插入元素操作,并在main函数中测试。

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

typedef struct node {
    int data;
    struct node *next;
} node;

node *create_circular_list(int value) {
    node *new_node = (node *)malloc(sizeof(node));
    new_node->data = value;
    new_node->next = new_node;
    return new_node;
}

void insert_element(node **head, int value) {
    node *new_node = (node *)malloc(sizeof(node));
    new_node->data = value;
    if (*head == NULL) {
        new_node->next = ______;
        *head = new_node;
    } else {
        new_node->next = (*head)->next;
        (*head)->next = new_node;
        *head = new_node;
    }
}

void print_list(node *head) {
    if (head == NULL) {
        printf("Empty list.\n");
        return;
    }

    node *current = head->next;
    do {
        printf("%d ", current->data);
        current = current->next;
    } while (current != head->next);
    printf("\n");
}

int main() {
    node *head = create_circular_list(1);
    insert_element(&head, 2);
    insert_element(&head, 3);
    print_list(head);
    return 0;
}

7.在做高精度整数减法运算时,在处理完前导0之后,我们可以利用<string.h>中的______函数比较不同长度的正整数的大小,利用______比较相同长度的正整数的大小。

8.在设计各种数据结构时,我们常使用结构体struct。假设有以下结构体定义:

struct example {
    char s[3];
    int i;
};

在32位计算机中,采用半字对齐,example结构体体的大小为______字节。

9.假设我们用一个数组 来实现一个队列, 的长度为 。每次入队操作时,若 已满,则需要创建一个新的长度为 的数组 ,将 中的元素复制到 中,然后继续执行入队操作。请问,执行 次入队操作的时间复杂度是______。

10.结构体类型的定义通常使用关键字struct。一个结构体可以包含多个成员,这些成员可以是不同的数据类型。例如,定义一个包含姓名(字符串类型)和年龄(整型)的结构体可以这样定义:struct person { char name[50]; int age; };。在这个结构体中,访问名为p的person结构体变量的年龄,可以使用______。

输出格式

共十行,每行一个字符串表示一道填空题的答案,请顺序答题,跳题请添加空行。

样例

【样例输出】(仅供格式参考,无需填写注释内容)

13         //第1小题答案
6          //第2小题答案
i++;       //第3小题答案
6 18       //第4小题答案
1,2,3,4,5  //第5小题答案
13         //第6小题答案
6          //第7小题答案
i++;       //第8小题答案
6 18 21 24 //第9小题答案
1,2,3,4,5  //第10小题答案
通告标题

通告内容

已知晓