题目描述
给定n个考场中所有考生的姓名、分数,输出这些考生的考场内排名。
注:
- 排名 = 高于当前分数的考生个数 + 1
- 分数相同时排名相同
输入描述
第一行一个整数n(1≤n≤10),表示考场个数;
接下来是n个考场的考生信息:
对每个考场,第一行为考场内的考生数量k(1≤k≤100),接下来k行,每行为一个考生的姓名name和分数score(name为仅由大小写字母组成的不超过15个字符的字符串,0≤score≤100),用空格隔开。数据确保全局不会出现相同的姓名。
输出描述
按考生姓名的字典序从小到大输出,每行为一个考生的姓名、分数、考场内排名,用空格隔开。
样例1
输入
2 4
SunWuKong 92
ShaWuJing 90
NiuMoWang 92
TangSanZang 100
2
BaiLongMa 90
ZhuBaJie 87
输出
BaiLongMa 90 1
NiuMoWang 92 2
ShaWuJing 90 4
SunWuKong 92 2
TangSanZang 100 1
ZhuBaJie 87 2
代码:
#include<iostream>
#include<algorithm>
using namespace std;
struct student{
string name;
int score;
int paiming; //排名
};
bool cmp(student a,student b){ //排序规则
return a.name<b.name;
}
bool cmp1(student a,student b){ //排序规则1
return a.score>b.score;
}
int main(){
int n;
cin>>n;
student stu[100*n];
int count=0; //记录总考生人数
for(int i=0;i<n;i++){
int k;
cin>>k;
int temp=k;
while(temp--){ //输入数据
cin>>stu[count].name>>stu[count].score;
count++;
}
sort(stu+(count-k),stu+count,cmp1); //按成绩排序
int common=1; //初始化相同分数的个数
int pai=1; //初始化排名
for(int j=count-k;j<count;j++){
if(j+1<count&&stu[j].score==stu[j+1].score){
common++; //相同数加一
}else{
for(int l=0;l<common;l++){
stu[j-l].paiming=pai;
}
pai+=common; //排名更新
common=1; //重置相同分数个数
}
}
}
sort(stu,stu+count,cmp); //按姓名排序
for(int i=0;i<count;i++){
cout<<stu[i].name<<" "<<stu[i].score<<" "<<stu[i].paiming<<endl;
}
return 0;
}
388

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



