题目链接:点击进入
解析:
题目不难,两次排序即可。有趣的点是这个
The testees with the same score must have the same rank, and the output must be sorted in nondecreasing order of their registration numbers.
同样成绩的人要有同样的排名。
for(int l=1;l<sum;l++){
if(stu[l].score != stu[l-1].score)
final_rank=l+1; //分数不同,final_rank为当前次序加1
printf("%s %d %d %d\n",stu[l].id, final_rank, stu[l].location_num, stu[l].local_rank);
}
完整代码:
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int max_N=100;
const int max_K=300;
typedef struct{
//long long id;
char id[13];
int score;
int location_num;
int local_rank;
}Stu;
Stu stu[max_N*max_K+10];
bool cmp(Stu a,Stu b){
if(a.score>b.score)
return true;
else if (a.score==b.score){
if(strcmp(a.id,b.id)<0)
return true;
else
return false;
}
return false;
}
int main(){
int N,K;
int sum=0;
scanf("%d",&N);
for(int i=1;i<=N;i++){
scanf("%d",&K);
for(int j=0;j<K;j++){
scanf("%s %d",stu[sum+j].id, &stu[sum+j].score);
stu[sum+j].location_num=i; //考场号
}
sort(stu+sum,stu+sum+K,cmp);
//接下来处理local_rank
stu[sum].local_rank=1;
for(int l=1;l<K;l++){
if(stu[sum+l].score==stu[sum+l-1].score)
stu[sum+l].local_rank=stu[sum+l-1].local_rank;
else
stu[sum+l].local_rank=l+1;
}
sum+=K;
}
//接下来处理final_rank
printf("%d\n",sum);
sort(stu,stu+sum,cmp);
int final_rank=1;
printf("%s %d %d %d\n",stu[0].id, final_rank, stu[0].location_num, stu[0].local_rank);
for(int l=1;l<sum;l++){
if(stu[l].score != stu[l-1].score)
final_rank=l+1; //分数不同,final_rank为当前次序加1
printf("%s %d %d %d\n",stu[l].id, final_rank, stu[l].location_num, stu[l].local_rank);
}
return 0;
}
不过我比较迷惑的是,当我的id用longlong实现时,有一个测试点不通过。