「PAT乙级真题解析」Basic Level 1041 考试座位号 (问题分析+完整步骤+伪代码描述+提交通过代码)
真正的题目是"有些考生迟到了,试机已经结束,他们只能拿着领到的试机座位号码求助于你,从后台查出他们的考试座位号码。" 所以这套题就是关于数据查询的问题。如何存以及如何查是解决问题的核心。
#pat考试#c语言#需求分析#算法#数据结构
Table of Contents
乙级的题目训练主要用来熟悉编程语言的语法和形成良好的编码习惯和编码规范。从小白开始逐步掌握用编程解决问题。
问题分析
真正的题目是"有些考生迟到了,试机已经结束,他们只能拿着领到的试机座位号码求助于你,从后台查出他们的考试座位号码。" 所以这套题就是关于数据查询的问题。如何存以及如何查是解决问题的核心。
如何存、如何查
我们先看要存的数据是什么类型和范围:
- 准考证号: 16位数字
- 用int存的话肯定是不够了
- 可以考虑存成字符串或者看使用的编程语言是否有足够范围的整数类型
- 试机座位号: 1~N (N <= 1000)
- 整型存储即可
- 考试座位号: 1~N (N <= 1000)
- 整型存储即可
然后我们需要考虑三个信息如何绑定在一起:
- 如果有类似键值对结构的数据类型可以直接使用, 比如JS的Object, python的Dict, C++的Map, C的结构体
- 比较麻烦一点的方案是, 准考证和考试座位号分别数组存储, 然后试机座位号作为数据在在数组中的对应索引.
完整描述步骤
- 获取参加考试的学生信息并存储数据:
- 准考证号
- 试机座位号
- 考试座位号
- 获取查询
- 对于每一个查询:
- 从存储数据中找到该考生信息存储位置
- 获取并输出: 准考证号 考试座位号
伪代码描述
- get info of participating students:
- exam_ID
- test_position (As index)
- formal_position And Store Them As structure Array or something like that.
- get queries and for each query:
- use test_position (index of stored data) find info of the student
- print(exam_ID, formal_position)
完整提交代码
/*
# 问题分析
真正的题目是"有些考生迟到了,试机已经结束,他们只能拿着领到的试机座位号码求助于你,从后台查出他们的考试座位号码。"
所以这套题就是关于数据查询的问题。如何存以及如何查是解决问题的核心。
# 如何存、如何查
我们先看要存的数据是什么类型和范围:
- 准考证号: 16位数字
- 用int存的话肯定是不够了
- 可以考虑存成字符串或者看使用的编程语言是否有足够范围的整数类型
- 试机座位号: 1~N (N <= 1000)
- 整型存储即可
- 考试座位号: 1~N (N <= 1000)
- 整型存储即可
然后我们需要考虑三个信息如何绑定在一起:
- 如果有类似键值对结构的数据类型可以直接使用, 比如JS的Object, python的Dict, C++的Map, C的结构体
- 比较麻烦一点的方案是, 准考证和考试座位号分别数组存储, 然后试机座位号作为数据在在数组中的对应索引.
# 完整描述步骤
1. 获取参加考试的学生信息并存储数据:
- 准考证号
- 试机座位号
- 考试座位号
2. 获取查询
3. 对于每一个查询:
- 从存储数据中找到该考生信息存储位置
- 获取并输出: 准考证号 考试座位号
# 伪代码描述
1. get info of participating students:
- exam_ID
- test_position (As index)
- formal_position
And Store Them As structure Array or something like that.
2. get queries and for each query:
- use test_position (index of stored data) find info of the student
- print(exam_ID, formal_position)
*/
# include<stdio.h>
# include<string.h>
typedef struct{
char exam_ID[17];
int test_position;
int formal_position;
} student;
int main(){
int student_amount;
scanf("%d\n", &student_amount);
student students[1001];
char exam_ID[17];
int test_position, formal_position;
for (int i = 0; i < student_amount; i++){
scanf("%s %d %d\n", exam_ID, &test_position, &formal_position);
strcpy(students[test_position].exam_ID, exam_ID);
students[test_position].test_position = test_position;
students[test_position].formal_position = formal_position;
}
int query_amount;
scanf("%d", &query_amount);
int query_ID;
for (int i = 0; i < query_amount; i++){
scanf("%d", &query_ID);
printf("%s %d\n", students[query_ID].exam_ID, students[query_ID].formal_position);
}
return 0;
}