题目
翻译
给他们进行排名,以A>C>M>E的顺序进行排列
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
结尾无空行
输入:N M
N个 ID C M E
M个ID
输出:
M个ID的最好成绩的顺序以及相应的名称
思路
读入N M
通过结构体进行存储数据
按照顺序读入成绩并计算平均分
进行排序sort 构造出cmp函数
!!!!一开始已经声明了flag,后面在for函数里面就不用声明直接用就好
注意:相同成绩的会是相同的排名
代码
```cpp
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
struct Student{
int id;
int score[4],rank[4];
int best_index;
}stu[2000];
int flag;
bool cmp(Student a,Student b){
return a.score[flag]>b.score[flag];
}
int exist[1000000]={0};
int main()
{ int N,M;
cin>>N>>M;
//读入成绩
for(int i=0;i<N;i++)
{
cin>>stu[i].id>>stu[i].score[1]>>stu[i].score[2]>>stu[i].score[3];
stu[i].score[0]=round((stu[i].score[1]+stu[i].score[2]+stu[i].score[3])/3.0);
//cout<<stu[i].id<<" "<<stu[i].score[0]<<endl;
}
for ( flag = 0; flag <= 3; flag++) {
// 根据这一项成绩排名
sort(stu,stu+N,cmp);
// 把根据这一分数的排名结果存进stu.rank[4]对应的那一栏
for (int i = 0; i < N; i++) {
// 这一项分数相同就并列,从i = 1开始要判断这个
if(i==0||stu[i].score[flag]!=stu[i-1].score[flag])
stu[i].rank[flag]=i+1;
else // 并列
stu[i].rank[flag] = stu[i - 1].rank[flag];
//cout<<stu[i].id<<" "<<stu[i].rank[flag]<<endl;
}
}
//在同一个id当中选出一个最好
for(int i=0;i<N;i++)
{
stu[i].best_index=0;
for(int j=1;j<4;j++)
{
if(stu[i].rank[j]<stu[i].rank[stu[i].best_index])
stu[i].best_index=j;
}
//cout<<stu[i].best_index<<" ";
exist[stu[i].id] = i+1;
}
int id;
char s[5]={'A','C','M','E'};
for(int i=0;i<M;i++)
{
cin>>id;
if(exist[id]==0)
cout<<"N/A"<<endl;
else {
int index=exist[id]-1;
cout<<stu[index].rank[stu[index].best_index]<<" "<<s[stu[index].best_index]<<endl;
}
}
return 0;
}