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 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 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
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 Input5 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 999999Sample Output
1 C 1 M 1 E 1 A 3 A N/A
主要思路:
1、使用map简历学生id与输入顺序号的联系
2、排名为实际排名,若有2个第一名,就没有第2名了
3、用一个二维数组存放每个学生的每科和平均成绩排名(不需要这样做,查哪个学生就去search这个学生的排名就好)
4、此代码通过PAT所有测试点,但是牛客网上一个测试点都通不过,why?
#include <iostream> #include <string> #include <map> using namespace std; #define Max_N 2001 void Rank_Per_Project(int* Array,int flag,int n,int (*Id)[6]) //将每一位学生某单个科目的排名存入Id二维数组的flag位 { int temp=0,count=1; for (int i=0; i<n; ++i) { temp=Array[i]; count=1; for (int k=0; k<n; ++k) { if (Array[k]>temp) { count++; } } Id[i][flag]=count; } } void Rank(int *C,int *M,int* E,int*A,int (*Id)[6],int n,string str) { Rank_Per_Project(C,1,n,Id); //处理每个学生C科目成绩的排名,并存入该学生Id数组 Rank_Per_Project(M,2,n,Id); //处理每个学生M科目成绩的排名,并存入该学生Id数组 Rank_Per_Project(E,3,n,Id); //处理每个学生E科目成绩的排名,并存入该学生Id数组 Rank_Per_Project(A,0,n,Id); //处理每个学生平均成绩的排名,并存入该学生Id数组 int rank=n+1; int flag=-1; for (int i=0; i<n; ++i) //处理每一个学生的最佳排名,并存入Id[][4],Id[][5]放入标志,Id二维数组的每行按课程优先级放置,低优先级的科目必须排名更好,才能记录到该学生的最佳排名 { rank=n+1; for (int k=0; k<4; ++k) { if (Id[i][k]<rank) { rank=Id[i][k]; flag=k; } } Id[i][4]=rank; Id[i][5]=flag; } } int main(int argc, const char * argv[]) { int n,m; cin>>n>>m; int C[Max_N]={0}; int M[Max_N]={0}; int E[Max_N]={0}; int A[Max_N]={0}; int Id[Max_N][6]={0}; map<string,int> relation;//创建每一个学生id与输入顺序号的关联,方便后续处理 string id; for (int i=0; i<n; ++i) { cin>>id>>C[i]>>M[i]>>E[i]; A[i]=C[i]+M[i]+E[i]; relation[id]=i; } string str="ACME"; Rank(C, M, E, A,Id,n,str); map<string,int>::iterator it; string search[Max_N]; int sequence=0; for (int i=0; i<m; ++i) { cin>>search[i]; } for (int i=0; i<m; ++i) { it=relation.find(search[i]); if (it!=relation.end())//成功找到该学生 { sequence=it->second; cout<<Id[sequence][4]<<' '<<str[Id[sequence][5]]<<endl;; }else//未找到该学生信息 { cout<<"N/A"<<endl; } } return 0; }