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.
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.
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
.
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
1 C
1 M
1 E
1 A
3 A
N/A
给出n个学生的id和三门成绩,再给出m个学生的id,要求输出这m个学生哪门课的排名最好。
/**
*@Author:hhzheng
*@Date:2023/2/13 7:28
*@Filename:PAT甲级1012 The Best Rank
*/
#include
#include
using namespace std;
struct node {
int id, best;
int score[4] = {-1};
int rank[4] = {-1};
} students[2001];
int exist[1000000], mark = -1;
bool cmp(node x, node y) {
return x.score[mark] > y.score[mark];
}
int main() {
int n, m, id;
cin >> n >> m;
/*获取所有学生的id 和 成绩信息,计算出平均值(四舍五入保留整数)*/
for (int i = 0; i < n; ++i) {
scanf("%d %d %d %d", &students[i].id, &students[i].score[1], &students[i].score[2], &students[i].score[3]);
students[i].score[0] = (students[i].score[1] + students[i].score[2] + students[i].score[3]) / 3.0 + 0.5;
}
/*对学生每门课的成绩进行排序,并给出排名*/
for ( mark = 0; mark <= 3; mark++) {
sort(students, students + n, cmp);
students[0].rank[mark] = 1;
/*防止出现1 1 2 3 4这样的排名 而是1 1 3 4 5这样的排名*/
for (int i = 1; i < n; i++) {
students[i].rank[mark] = i + 1;
if (students[i].score[mark] == students[i - 1].score[mark])
students[i].rank[mark] = students[i - 1].rank[mark];
}
}
/*对每个学生的排名进行排名 找出最好的那个*/
for (int i = 0; i < n; i++) {
exist[students[i].id] = i + 1;
students[i].best = 0;
int minn = students[i].rank[0];
for (int j = 1; j <= 3; j++) {
if (students[i].rank[j] < minn) {
minn = students[i].rank[j];
students[i].best = j;
}
}
}
/*获取需要输出排名的学生id 如果在exist数组中存在说明之前录入过成绩 此时输入其最好的排名
* 如果exist数组中不存在这个id 则输出N/A*/
char c[5] = {'A', 'C', 'M', 'E'};
for (int i = 0; i < m; i++) {
scanf("%d", &id);
int temp = exist[id];
if (temp) {
int best = students[temp - 1].best;
printf("%d %c\n", students[temp - 1].rank[best], c[best]);
} else {
printf("N/A\n");
}
}
return 0;
}
这题虽然有思路,这个思路和柳神的解法还很接近但是就是缺乏把想法实现的操作,还是需要多多练习。我把链接贴在下边:
https://www.liuchuo.net/archives/2207