1012. The Best Rank (25)

来源:互联网 发布:张予曦淘宝店质量 编辑:程序博客网 时间:2024/05/18 01:28

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 Algebra), 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  A310101     98 85 88 90310102     70 95 88 84310103     82 87 94 88310104     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

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

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 6310101 98 85 88310102 70 95 88310103 82 87 94310104 91 91 91310105 85 90 90310101310102310103310104310105999999
Sample Output
1 C1 M1 E1 A3 AN/A


分析:

(1)本题考查排序,由于直接调用了快排的算法,导致外部的处理有点冗余。

(2)思路:建立Node结构体,并建立Node型数组ND。根据每项成绩对数组排序,并相应的设置排名。调整排名数组的顺序(因为有优先考虑),找出最小值。输出每个结构数组单元的最小(高)排名,以及改项名称。    总结:输入,排序,最值,输出。

(3)注意回顾快速排序的用法,同时疑问的是,cp函数中,<=改造<,会出现段错误。和朋友未讨论出原因。

(4)代码很冗余,不知道如何改。 

#include <stdio.h>#include <string>#include <iostream>#include <algorithm>using namespace std;struct Node{string ID;int score[4];int rank[4];int best;};bool cp1 (Node formal, Node latter){if (formal.score[0]<=latter.score[0]){return false;}else return true;}bool cp2 (Node formal, Node latter){if (formal.score[1]<=latter.score[1]){return false;}else return true;}bool cp3 (Node formal, Node latter){if (formal.score[2]<=latter.score[2]){return false;}else return true;}bool cp4 (Node formal, Node latter){if (formal.score[3]<=latter.score[3]){return false;}else return true;}char C[4]={'A','C','M','E'};int main(int argc, char *argv[]){int N,M,i,j;cin>>N;cin>>M;Node ND[2001];string ps[2001];//Node *ND=new Node [N];//string *ps=new string [M];for (i=0;i<N ;i++ ){cin>>ND[i].ID;scanf("%d %d %d",&ND[i].score[0],&ND[i].score[1],&ND[i].score[2]);        ND[i].score[3]=(ND[i].score[0]+ND[i].score[1]+ND[i].score[2])/3;}    sort (&ND[0],&ND[N],cp1);for (i=0;i<N ;i++ ){ND[i].rank[0]=i+1;if (i!=0&&ND[i].score[0]==ND[i-1].score[0]){ND[i].rank[0]=ND[i-1].rank[0];}}sort (&ND[0],&ND[N],cp2);for (i=0;i<N ;i++ ){ND[i].rank[1]=i+1;if (i!=0&&ND[i].score[1]==ND[i-1].score[1]){ND[i].rank[1]=ND[i-1].rank[1];}}sort (&ND[0],&ND[N],cp3);for (i=0;i<N ;i++ ){ND[i].rank[2]=i+1;if (i!=0&&ND[i].score[2]==ND[i-1].score[2]){ND[i].rank[2]=ND[i-1].rank[2];}}sort (&ND[0],&ND[N],cp4);for (i=0;i<N ;i++ ){ND[i].rank[3]=i+1;if (i!=0&&ND[i].score[3]==ND[i-1].score[3]){ND[i].rank[3]=ND[i-1].rank[3];}}for (i=0;i<N ;i++ ){   ND[i].best=0;int temp=ND[i].rank[3];ND[i].rank[3]=ND[i].rank[2];// 重要性顺序优先排序ND[i].rank[2]=ND[i].rank[1];ND[i].rank[1]=ND[i].rank[0];ND[i].rank[0]=temp;for (j=0;j<4 ;j++ ){if (ND[i].rank[j]<ND[i].rank[ND[i].best]){ND[i].best=j;    }}}for (i=0;i<M ;i++ ){cin>>ps[i];}    for (i=0;i<M ;i++ ) {   for (j=0;j<N ;j++ ){if (ps[i]==ND[j].ID){printf("%d %c\n",ND[j].rank[ND[j].best],C[ND[j].best]);break;}    else if (j==N-1&&ps[i]!=ND[j].ID){printf("N/A\n");    }}     }//delete [] ps;//delete [] ND;return 0;}


0 0