PAT 甲级 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
Experiential Summing-up
1、用结构体储存比较方便
2、输入完学生信息(学号、成绩)后将每门课按成绩排序,并处理并列情况——排名并列应该为1、1、3、4、5,而不是1、1、2、3、4
3、然后寻找每位学生的最高排名并输出
4、其他注意事项在代码中注释
Accepted Code
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n, m, id, flag = -1; 4 char c[4] = {'A', 'C', 'M', 'E'}; 5 int exist[1000000]; //用来保存当前id是否存在 6 struct node 7 { 8 int id, best;//best记录最好的科目是哪一门(0、1、2、3) 9 int score[4], rank[4]; 10 }stu[2005]; 11 12 bool cmpl(node a, node b) 13 { 14 return (a.score[flag] > b.score[flag]); 15 } 16 17 int main() 18 { 19 cin >> n >> m; 20 for(int i = 0; i < n; i ++) 21 { 22 cin >> stu[i].id >> stu[i].score[1] >> stu[i].score[2] >> stu[i].score[3]; 23 stu[i].score[0] = (stu[i].score[1] + stu[i].score[2] + stu[i].score[3]) / 3.0; 24 } 25 for(flag = 0; flag <= 3; flag ++) 26 { 27 sort(stu, stu + n, cmpl); 28 stu[0].rank[flag] = 1; //令排序后第一位同学的第flag门成绩排名第一 29 for(int i = 1; i < n; i ++) 30 { 31 stu[i].rank[flag] = i + 1; //后面同学排名依次+1 32 if(stu[i].score[flag] == stu[i-1].score[flag]) //处理并列情况 33 stu[i].rank[flag] = stu[i-1].rank[flag]; 34 } 35 } 36 for(int i = 0; i < n; i ++) 37 { 38 exist[stu[i].id] = i + 1; //因为下面需要判断temp!=0,所以exist从第一位开始存 39 stu[i].best = 0; 40 int minn = stu[i].rank[0]; 41 for(int j = 1; j <= 3; j ++) //1? 42 {//寻找每位学生的最高排名 43 if(stu[i].rank[j] < minn) 44 { 45 minn = stu[i].rank[j]; 46 stu[i].best = j; 47 } 48 } 49 } 50 for(int i = 0; i < m; i ++) 51 { 52 cin >> id; 53 int temp = exist[id]; 54 if(temp) 55 { 56 int best = stu[temp-1].best; //temp-1与结构体对应 57 cout << stu[temp-1].rank[best] << " " << c[best] << endl; 58 } 59 else cout << "N/A" << endl; 60 } 61 return 0; 62 }