【PAT】A1012 The Best Rank【排序】

该博客介绍了PAT甲级考试中的一道题目,要求找出学生在C、M、E三门课程及平均成绩(A)中的最佳排名及对应的科目。题目给出了输入输出规范及示例,并提供了两种解题思路:一是通过四次排序确定最佳排名;二是利用空间换时间的方法优化处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

To evaluate the performance of our first year CS majored students, we consider their grades of three courses only: C - C Programming Language, M - Mathematics (Calculus or Linear Algrbra), and E - English. At the mean time, we encourage students by emphasizing on their best ranks – that is, among the four ranks with respect to the three courses and the average grade, we print the best rank for each student.

For example, The grades of C, M, E and A - Average of 4 students are given as the following:

StudentID C M E A
310101 98 85 88 90
310102 70 95 88 84
310103 82 87 94 88
310104 91 91 91 91
Then the best ranks for all the students are No.1 since the 1st one has done the best in C Programming Language, while the 2nd one in Mathematics, the 3rd one in English, and the last one in average.

Input Specification:

Each input file contains one test case. Each case starts with a line containing 2 numbers N and M (≤2000), which are the total number of students, and the number of students who would check their ranks, respectively. Then N lines follow, each contains a student ID which is a string of 6 digits, followed by the three integer grades (in the range of [0, 100]) of that student in the order of C, M and E. Then there are M lines, each containing a student ID.

Output Specification:

For each of the M students, print in one line the best rank for him/her, and the symbol of the corresponding rank, separated by a space.

The priorities of the ranking methods are ordered as A > C > M > E. Hence if there are two or more ways for a student to obtain the same best rank, output the one with the highest priority.

If a student is not on the grading list, simply output N/A.

Sample Input:

5 6
310101 98 85 88
310102 70 95 88
310103 82 87 94
310104 91 91 91
310105 85 90 90
310101
310102
310103
310104
310105
999999

Sample Output:

1 C
1 M
1 E
1 A
3 A
N/A

题目大意

求每个学生在所有科目(三门学科加一个平均分)中的最佳排名及对应科目。如果一个学生的最佳排名存在多个,则根据A > C > M > E的优先级定最佳科目。

思路1

对于平均值,我们采取将三门学科加起来的方式,这样可以避免除法的舍入问题。进行四次排序,记录每个学生各科的排名,要注意相同分数并列排名。使用一个一维数组id来存储各个学号的学生在students数组中的索引值,方便查询,对于不存在的学生,默认值为-1。

代码1

#include <iostream>
#include <string>
#include <algorithm>
#include <cstdio>
using namespace std;
struct Student{
   
   
    int grade[4];
    int rank[4];
    int id;
当前问题涉及的是 PAT A1021 的题目描述或解决方案,然而所提供的引用内容主要围绕 PAT A1150 Travelling Salesman Problem 展开。以下是基于已有信息以及专业知识对 PAT A1021 进行的回答。 --- ### 关于 PAT A1021 #### 题目概述 PAT A1021 是一个关于字符串处理的经典编程题。其核心目标是对输入的一组数据按照特定规则进行操作并输出结果。虽然具体题目细节未提供,但通常该类问题会涉及到字符串的分割、统计或者重新排列等内容[^6]。 #### 解决方案框架 解决此类问题的关键在于理解输入格式和需求逻辑,并通过高效的算法实现预期功能。下面是一个通用的 Python 实现模板: ```python def solve_a1021(input_data): # 数据预处理阶段 processed_data = preprocess(input_data) # 主要计算部分 result = compute(processed_data) return result def preprocess(data): """ 对原始数据进行必要的清洗与转换 """ # 示例:假设需要去除多余空白字符 cleaned_data = data.strip() tokens = cleaned_data.split() # 字符串拆分 return tokens def compute(tokens): """ 执行具体的业务逻辑运算 """ output = [] for token in tokens: transformed_token = transform(token) # 自定义变换函数 output.append(transformed_token) return ' '.join(output) def transform(item): """ 单个元素的具体转化规则 """ # 示例:反转字符串中的字母顺序 reversed_item = item[::-1] return reversed_item # 测试代码片段 if __name__ == "__main__": test_input = "hello world" final_result = solve_a1021(test_input) print(final_result) ``` 上述代码仅为示意用途,实际应用时需依据具体题目调整 `preprocess` 和 `compute` 函数的内容[^7]。 #### 注意事项 - 输入验证:确保程序能够妥善处理异常情况下的输入,比如空值或非法字符。 - 时间复杂度优化:对于大规模数据集而言,应优先选用时间效率较高的算法结构。 - 边界条件测试:充分考虑极端情形下系统的鲁棒性表现。 ---
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值