题目:
有n个考场,每个考场有若干数量的考生。现在给出各个考场中的考生的准考证号与分数,要求所有考生按照考生分数从高到低排序,并按顺序输出所有考生的准考证号,排名,考场号以及考场内排名。
思路:
在student结构体类型中存放题目信息(准考证号,分数,考场号,考场内排名)
分数不相同时,按照分数从大到小排,否则按照学号从小到大排序
代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct Student{
char id[15]; //准考证号
int score; //分数
int location_number; //考场号
int local_rank; //考场内排名
}stu[30010];
bool cmp(Student a, Student b)
{
if(a.score != b.score) return a.score > b.score; //先按分数大小排序
else return strcmp(a.id, b.id); //分数相同按照id准考证号排序
}
int main()
{
int n, k, num=0; //num总考生数
scanf("%d", &n); //n考场数
for(int i=0; i<=n; i++){
scanf("%d", &k); //该考场内人数
for(int j=0; j<=k; j++){
scanf("%s %d", stu[num].id, &stu[num].score);
stu[num].location_number = i; //该考生的考场号为i
num++; //总考生数加1
}
sort(stu+num-k, stu+num, cmp); //将该考场的考生排序
stu[num-k].local_rank = 1; //该考场第1名的local_rank记为1
for(int j=num-k+1; j<num; j++){ //对该考场剩余的考生
if(stu[j].score == stu[j-1].score) //如果与前一位考生同分
{
stu[j].local_rank = stu[j-1].local_rank;
}else{ //不同分
stu[j].local_rank = j+1-(num-k);
}
}
}
printf("%d\n", num); //输出总考生数
sort(stu, stu+num, cmp); //将所有考生排名
int r=1; //当前考生排名
for(int i=0; i<num; i++)
{
if(i>0&&stu[i].score != stu[i-1].score){
r = i+1; //更新当前考生排名
}
printf("%s", stu[i].id);
printf("%d%d%d\n", r,stu[i].location_number, stu[i].local_rank);
}
return 0;
}