这道题的坑点在于需要注意输出的并列排名,比如90、60、60、50的对应排名应该是1、2、2、4,很容易搞成1、2、3、4千万注意这一点。
#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
#include <string>
using namespace std;
struct student{
string StudentID;
int score[4];
int bestRank;
int currRank;
char bestRankLable;
student();
};
student::student(){
bestRank=2001;
}
char lables[]={'A','C','M','E'};
int cmp0(const student A,const student B){
return A.score[0]>B.score[0];
}
int cmp1(const student A,const student B){
return A.score[1]>B.score[1];
}
int cmp2(const student A,const student B){
return A.score[2]>B.score[2];
}
int cmp3(const student A,const student B){
return A.score[3]>B.score[3];
}
vector <student> getBestRank(vector <student> &vS,int N,int lable){
vS[0].currRank=1;
if(vS[0].bestRank>1){
vS[0].bestRank=1;
vS[0].bestRankLable=lables[lable];
}
for(int i=1;i<N;i++){
if(vS[i].score[lable]==vS[i-1].score[lable]){
vS[i].currRank=vS[i-1].currRank;
}else{
vS[i].currRank=i+1;
}
if(vS[i].bestRank>vS[i].currRank){
vS[i].bestRank=vS[i].currRank;
vS[i].bestRankLable=lables[lable];
}
}
return vS;
}
int main(int argc, char** argv) {
int N,M;
map<string,student> mStudent;
vector<student> vStudent;
student temp;
string id;
cin>>N>>M;
for(int i=0;i<N;i++){
cin>>temp.StudentID>>temp.score[1]>>temp.score[2]>>temp.score[3];
temp.score[0]=(temp.score[1]+temp.score[2]+temp.score[3]);
vStudent.push_back(temp);
}
sort(vStudent.begin(),vStudent.end(),cmp0);
getBestRank(vStudent,N,0);
sort(vStudent.begin(),vStudent.end(),cmp1);
getBestRank(vStudent,N,1);
sort(vStudent.begin(),vStudent.end(),cmp2);
getBestRank(vStudent,N,2);
sort(vStudent.begin(),vStudent.end(),cmp3);
getBestRank(vStudent,N,3);
for(int i=0;i<N;i++){
mStudent[vStudent[i].StudentID]=vStudent[i];
}
for(int i=0;i<M;i++){
cin>>id;
if(mStudent.count(id)>0){
cout<<mStudent[id].bestRank<<" "<<mStudent[id].bestRankLable<<endl;
}else{
cout<<"N/A"<<endl;
}
}
}