PAT (Advanced) 1025. PAT Ranking (25)

原题:1025. PAT Ranking (25)


解题思路:简单的模拟,对每一组数据进行排序,记录考场排名,最后对所有数据进行排序,记录最终排名



c++代码如下:

#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
struct Student{
    char id[15];
    int score;
    int final_rank;
    int lc_rank;
    int lc_num;
};
vector<Student> stu;
bool cmp(Student a, Student b)
{
    if(a.score != b.score)
        return a.score > b.score;
    else
        return strcmp(a.id, b.id) < 0;
}


int main()
{
    int k;
    while(scanf("%d", &k) != EOF)
    {
        if(k == 0)
            printf("0\n");
        else
        {
            int cnt = 0;//记录考生总数
            for(int i = 0; i < k; i++)
            {
                int n;
                int r = 1;
                Student x;
                scanf("%d", &n);
                //对每一小组的处理
                for(int j = 0; j < n; j++)
                {
                    scanf("%s", x.id);
                    scanf("%d", &x.score);
                    x.lc_num = i + 1;
                    stu.push_back(x);
                }
                sort(stu.begin() + cnt, stu.begin() + cnt + n, cmp);
                stu[cnt].lc_rank = 1;
                for(int j = cnt + 1; j < cnt + n; j++)
                {
                    if(stu[j].score == stu[j-1].score) //分数相同时 排名相同
                        stu[j].lc_rank = stu[j-1].lc_rank;
                    else
                        stu[j].lc_rank = j - cnt + 1;
                }

                cnt += n; //加上本组考生
            }

            //对总体排名进行计算
            sort(stu.begin(), stu.end(), cmp);
            stu[0].final_rank = 1;
            for(int i = 1; i < cnt; i++)
            {
                if(stu[i].score == stu[i-1].score)
                    stu[i].final_rank = stu[i-1].final_rank;
                else
                    stu[i].final_rank = i + 1;
            }
            //输出
            printf("%d\n", cnt);
            for(int i = 0; i < cnt; i++)
            {
                printf("%s %d %d %d\n", stu[i].id, stu[i].final_rank, stu[i].lc_num, stu[i].lc_rank);
            }
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值