PAT A1012 The Best Rank
Posted mrdragon
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PAT A1012 The Best Rank相关的知识,希望对你有一定的参考价值。
PAT A1012 The Best Rank
题目描述:
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
参考代码:
1 /************************************************** 2 PAT A1012 The Best Rank 3 **************************************************/ 4 #include <iostream> 5 #include <cmath> 6 #include <algorithm> 7 #include <string> 8 #include <vector> 9 10 using namespace std; 11 12 struct StudInfo 13 string id = ""; 14 int score[4] = 0 ; 15 int rank[4] = 0 ; 16 ; 17 18 bool comByC(StudInfo a, StudInfo b) return a.score[0] > b.score[0]; 19 bool comByM(StudInfo a, StudInfo b) return a.score[1] > b.score[1]; 20 bool comByE(StudInfo a, StudInfo b) return a.score[2] > b.score[2]; 21 bool comByA(StudInfo a, StudInfo b) return a.score[3] > b.score[3]; 22 23 bool (*cmp[4])(StudInfo, StudInfo) = comByC, comByM, comByE, comByA ; 24 25 void printBestRank(int aRank, int cRank, int mRank, int eRank) 26 if (aRank <= cRank && aRank <= mRank && aRank <= eRank) cout << aRank << " A"; 27 else if (aRank > cRank && cRank <= mRank && cRank <= eRank) cout << cRank << " C"; 28 else if (aRank > mRank && cRank > mRank && mRank <= eRank) cout << mRank << " M"; 29 else cout << eRank << " E"; 30 31 32 int main() 33 int stuCount = 0, checkCount = 0; 34 35 cin >> stuCount >> checkCount; 36 37 vector<StudInfo> stuList(stuCount); 38 39 //获取三门课的成绩并计算平均成绩 40 for (int i = 0; i < stuCount; ++i) 41 cin >> stuList[i].id >> stuList[i].score[0] >> stuList[i].score[1] >> stuList[i].score[2]; 42 stuList[i].score[3] = round((stuList[i].score[0] + stuList[i].score[1] + stuList[i].score[2])) / 3.0 + 0.5; 43 44 45 //分别按照每门课进行排序 46 for (int i = 0; i < 4; ++i) 47 sort(stuList.begin(), stuList.end(), cmp[i]); 48 49 stuList[0].rank[i] = 1; 50 for (int j = 1; j < stuCount; ++j) 51 stuList[j].rank[i] = stuList[j].score[i] == stuList[j - 1].score[i] ? stuList[j - 1].rank[i] : j + 1; 52 53 54 55 //输出查询的结果 56 string checkStuId; 57 for (int i = 0; i < checkCount; ++i) 58 bool findFlag = false; //是否找到要查询的学生的信息 59 60 cin >> checkStuId; 61 62 for (int j = 0; j < stuCount; ++j) 63 if (checkStuId == stuList[j].id) 64 printBestRank(stuList[j].rank[3], stuList[j].rank[0], stuList[j].rank[1], stuList[j].rank[2]); 65 findFlag = true; 66 break; 67 68 69 70 if (!findFlag) cout << "N/A"; 71 72 if (i != checkCount - 1) cout << endl; //换行 73 74 75 return 0; 76
注意事项:
1:各科排名的比较函数可以放入函数指针数组。
2:注意如下的情况:100, 98, 98, 97。此时第2、3位置分数相同都是第二名,但是第4位置的97的同学它的排名不是第三名而是第四名!
以上是关于PAT A1012 The Best Rank的主要内容,如果未能解决你的问题,请参考以下文章