1012 The Best Rank
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
审题
题目给出一组学生成绩信息,包含学号和三门成绩——[id,C,M,E],计算学生的平均分A。然后按各门成绩(包括平均分)排名。最后输入一组学生的学号,输出每个学生四门成绩中排名最好的排名和科目。如果某学生有相同排名的科目按优先级A>C>M>E输出。
思路
这是一个典型的排序题。用结构体数组存储学生信息和排名信息,按要求进行四次排序。最后输出结果。
主要函数和功能模块
-
结构体存储学生信息,结构体包含字段[id,A,C,M,E,R[4]]。id——学号;A——平均分;C——C课程成绩;M——M课程成绩;E——E课程成绩;R[]——各课程成绩排名(按题中优先级存储)。
-
排序直接调用sort()函数,需要自己写各次排序的cmp。
-
每次排序后更新学生该科目成绩排名,注意分数相同的排名相同。
-
查询函数search(int id),函数需要处理1.学号能否查到;2.求最好排名;3.排名相同的科目按优先级输出
参考代码
1 #include<iostream> 2 #include<string> 3 #include<string.h> 4 #include<algorithm> 5 using namespace std; 6 7 typedef struct { 8 string id; 9 int A,C,M,E; 10 int R[4]; 11 }Stu; 12 13 Stu s[10000]; 14 int n,m; 15 //课程类别数组,按优先级存储 16 char course[4]={'A','C','M','E'}; 17 18 bool cmp1(Stu a,Stu b) 19 { 20 return a.A>b.A; 21 } 22 23 bool cmp2(Stu a,Stu b) 24 { 25 return a.C>b.C; 26 } 27 28 bool cmp3(Stu a,Stu b) 29 { 30 return a.M>b.M; 31 } 32 33 bool cmp4(Stu a,Stu b) 34 { 35 return a.E>b.E; 36 } 37 38 39 40 void search(string id) 41 { 42 int tag=-1; 43 int flag=-1; 44 int min=1000000000; 45 for(int i=0;i<n;i++) 46 { 47 if(strcmp(id.c_str(),s[i].id.c_str())==0){ 48 tag=i; 49 break; 50 } 51 } 52 if(tag==-1){ 53 cout<<"N/A"<<endl; 54 return; 55 } 56 for(int i=0;i<4;i++){ 57 if(s[tag].R[i]<min){ 58 min=s[tag].R[i]; 59 flag=i; 60 } 61 } 62 cout<<min<<" "<<course[flag]<<endl; 63 } 64 65 int main() 66 { 67 cin>>n>>m; 68 for(int i=0;i<n;i++){ 69 cin>>s[i].id>>s[i].C>>s[i].M>>s[i].E; 70 s[i].A=(s[i].C+s[i].M+s[i].E)/3; 71 } 72 sort(s,s+n,cmp1); 73 //更新排名 74 for(int i=0;i<n;i++){ 75 if(i==0){ 76 s[i].R[0]=1; 77 } else if(s[i-1].A==s[i].A){ 78 //R[]的下标要注意与sort()函数和优先级对应起来。之前这里粗心犯了错误. 79 s[i].R[0]=s[i-1].R[0]; 80 } else { 81 s[i].R[0]=i+1; 82 } 83 84 } 85 sort(s,s+n,cmp2); 86 for(int i=0;i<n;i++){ 87 if(i==0){ 88 s[i].R[1]=1; 89 } else if(s[i-1].C==s[i].C){ 90 s[i].R[1]=s[i-1].R[1]; 91 } else { 92 s[i].R[1]=i+1; 93 } 94 95 } 96 sort(s,s+n,cmp3); 97 for(int i=0;i<n;i++){ 98 if(i==0){ 99 s[i].R[2]=1; 100 } else if(s[i-1].M==s[i].M){ 101 s[i].R[2]=s[i-1].R[2]; 102 } else { 103 s[i].R[2]=i+1; 104 } 105 106 } 107 sort(s,s+n,cmp4); 108 for(int i=0;i<n;i++){ 109 if(i==0){ 110 s[i].R[3]=1; 111 } else if(s[i-1].E==s[i].E){ 112 s[i].R[3]=s[i-1].R[3]; 113 } else { 114 s[i].R[3]=i+1; 115 } 116 117 } 118 for(int i=0;i<m;i++){ 119 string id; 120 cin>>id; 121 search(id); 122 } 123 }
调用sort()之后更新排名没有与排序的元素相对应。粗心犯错,以后在多次排序的题目中用sort()要搞清楚排序的对象,仔细检查。

浙公网安备 33010602011771号