酷代码 AI
菜单
服务商

PAT 考试座位号查询:C 语言实现及复杂度分析

```c #include <stdio.h> #define MAX_N 1005 #define ID_LEN 17 // 定义结构体来存储考生信息 typedef struct { char id[ID_LEN]; int testSeat; int examSeat; } Candidate; int main() { int n, m; // 存储考生信息的数组 Candidate candidates[MAX_N]; // 存储待查询的试机座位号 int querySeats[MAX_N]; // 读取考生信息 scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%s %d %d", candidates[i].id, &candidates[i].testSeat, &candidates[i].examSeat); } // 读取待查询的试机座位号数量 scanf("%d", &m); for (int i = 0; i < m; i++) { scanf("%d", &querySeats[i]); } // 遍历待查询的试机座位号,查找对应的考生信息并输出 for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (candidates[j].testSeat == querySeats[i]) { printf("%s %d\n", candidates[j].id, candidates[j].examSeat); break; } } } return 0; } ``` 代码解释: 1. 结构体定义:定义了 `Candidate` 结构体来存储每个考生的准考证号、试机座位号和考试座位号。 2. 输入处理: - 首先读取考生数量 `n`,然后循环读取每个考生的准考证号、试机座位号和考试座位号,存储在 `candidates` 数组中。 - 接着读取待查询的试机座位号数量 `m`,并将这些试机座位号存储在 `querySeats` 数组中。 3. 查询处理:遍历 `querySeats` 数组,对于每个待查询的试机座位号,遍历 `candidates` 数组,找到对应的考生信息并输出准考证号和考试座位号。 复杂度分析: - 时间复杂度:$O(N \times M)$,其中 $N$ 是考生数量,$M$ 是待查询的试机座位号数量。 - 空间复杂度:$O(N)$,主要用于存储考生信息。 ######[AI问答 | 581点数解答 | 2025-12-12 20:22:58]

相关提问
本站限时免费提问中
实用工具查看更多
Linux在线手册 [开发类]
Jquery在线手册 [开发类]
今日油价 [生活类]
图片互转base64 [开发类]
时间转换器 [开发类]