The Best Rank
- 设置 student 结构体,成绩用数组表示
- 按照不同的成绩排序 stu 数组,用ranking 的二维数组记录四种排名
- 注意:排名存在并列,当当前学生的排名和之前的学生的排名相同时,不能按照序号来排名
1 #include <iostream> 2 #include <algorithm> 3 #include <string> 4 #include <map> 5 using namespace std; 6 7 8 const int maxn=2010; 9 struct stu{ 10 int id; 11 int grades[4]; 12 }students[maxn]; 13 char course[4]={'A','C','M','E'}; 14 int ranking[10000000][4]; 15 int now; 16 17 bool cmp(stu a, stu b){ 18 return a.grades[now]>b.grades[now]; 19 } 20 21 int main(){ 22 int len, time; 23 cin>>len>>time; 24 for(int i=0;i<len;i++){ 25 cin>>students[i].id>>students[i].grades[1]>>students[i].grades[2]>>students[i].grades[3]; 26 students[i].grades[0]=(students[i].grades[1]+students[i].grades[2]+students[i].grades[3])/3; 27 } 28 for(now =0;now<4;now++){ 29 sort(students,students+len,cmp); 30 ranking[students[0].id][now] = 1; 31 for(int i=1;i<len;i++){ 32 int pre=students[i-1].id; 33 int p=students[i].id; 34 if(students[i].grades[now]==students[i-1].grades[now])ranking[p][now]=ranking[pre][now]; 35 else ranking[p][now]=i+1; 36 // cout<<"看这里 id:"<<p<<' '<<ranking[p][now]<<endl; 37 } 38 } 39 40 for(int query=0;query<time;query++){ 41 int quest; 42 cin>>quest; 43 int bestrank=maxn; 44 int bestcourse; 45 for(int i=0;i<4;i++){ 46 if(ranking[quest][i]<bestrank){ 47 bestrank=ranking[quest][i]; 48 bestcourse=i; 49 } 50 } 51 if(bestrank==0)cout<<"N/A"<<endl; 52 else{ 53 cout<<bestrank<<' '<<course[bestcourse]<<endl; 54 } 55 } 56 57 return 0; 58 }