PAT 1025|PAT Ranking

题目链接:点击进入

解析:

题目不难,两次排序即可。有趣的点是这个
  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实现时,有一个测试点不通过。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值