1012 The Best Rank
题目链接
https://pintia.cn/problem-sets/994805342720868352/problems/994805502658068480
解题思路
1.显而易见,这题用结构体是很方便的;我们只需要在每一次比较结束之后对各个学生的名次进行排序;
2.对学生名次排序:第一名肯定是 1 ,但是第 i (i>1)名不一定是 i ,需要与i - 1名进行比较;
总结
sort排序可以写四个,也可以简写,用全局变量化简。
代码展示
#include<bits/stdc++.h>
using namespace std;
/*
*/
struct stu{
int id;
double score[5];
int Rank[5];
};
stu student[2005];
int now;
bool cmp(stu a,stu b){
return a.score[now]>b.score[now];
}
int rank1[1000000][5];
char sco[4]={'C','M','E','A'};
int main(){
int n,m;
cin>>n>>m;
for(int i=0;i<n;i++){
cin>>student[i].id;
double sum=0;
for(int j=0;j<3;j++){
cin>>student[i].score[j];
sum+=student[i].score[j];
}
student[i].score[3]=sum/3;
}
for(now =0;now<4;now++){
sort(student,student+n,cmp);
rank1[student[0].id][now]=1;
for(int i=1;i<n;i++){
if(student[i].score[now]==student[i-1].score[now])
rank1[student[i].id][now]=rank1[student[i-1].id][now];
else rank1[student[i].id][now]=i+1;
}
}
while(m--){
int id;
cin>>id;
if(!rank1[id][0])
cout<<"N/A"<<endl;
else {
int k=3;
for(int i=0;i<4;i++){
if(rank1[id][i]<rank1[id][k])
k=i;
}
cout<<rank1[id][k]<<" "<<sco[k]<<endl;
}
}
return 0;
}
该博客介绍了一种C++解决方案,用于处理竞赛中学生的排名情况。通过定义结构体stu,存储每个学生的信息,包括ID、分数和名次,并使用sort函数进行排序。在处理相同分数时,代码能正确计算并输出每个学生的具体排名。此外,博客还展示了如何根据输入查询特定学生的最佳科目排名。
859

被折叠的 条评论
为什么被折叠?



