PAT (Advanced Level) 1012 The Best Rank (25分)
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
这题有个2个坑
1.一个是平均分不要用double,直接转成整型,但是不能直接整除,要四舍五入
2.相同分数排名相同 即可能存在 1 1 3 4 5 6.这中排名,不然会掉2个点
#include <bits/stdc++.h>
using namespace std;
struct Score{
int best, bestid, no;
int v[4], rd[4];
};
int exist[1000005];
Score student[2005];
int n, m, id;
string p = "ACME";
bool cmp(Score a, Score b){return a.v[id] > b.v[id]; }
bool cmpid(Score a, Score b){return a.no < b.no; }
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
for (int i = 0;i < n; i++){
int s;
cin >> s;
exist[s] = i + 1;
student[i].no = i;
for (int j = 1; j <= 3; j++) cin >> student[i].v[j], student[i].v[0] += student[i].v[j];
student[i].v[0] = (int)(1.0 * student[i].v[0] / 3);
}
for (id = 0; id <= 3; id++){
sort(student, student + n, cmp);
student[0].rd[id] = 1;
for (int i = 1; i < n; i++){
student[i].rd[id] = i + 1;
if (student[i].v[id] == student[i - 1].v[id]) student[i].rd[id] = student[i - 1].rd[id];
}
}
sort(student , student + n ,cmpid);
for (int i = 0; i < n; i++){
student[i].best = student[i].rd[0];
student[i].bestid = 0;
for (id = 1; id <= 3; id++){
if (student[i].rd[id] < student[i].best){
student[i].best = student[i].rd[id];
student[i].bestid = id;
}
}
}
for (int i = 0; i < m; i++){
int s;
cin >> s;
int _i = exist[s];
if (_i == 0) cout << "N/A\n";
else{
cout << student[_i-1].best << " " << p[student[_i-1].bestid] << "\n";
}
}
return 0;
}
本文详细解析了PAT(Advanced Level)1012题目《The Best Rank》的算法实现,介绍了如何处理学生在三门课程上的成绩,计算其在各科及平均成绩的最佳排名,并考虑了相同分数排名相同的情况,确保排名公正。文章提供了完整的C++代码示例,展示了如何使用结构体、排序和条件判断来解决复杂排名问题。
359

被折叠的 条评论
为什么被折叠?



