PAT(A)1025 PAT Ranking (25分)

在这里插入图片描述

Sample Input

2
5
1234567890001 95
1234567890005 100
1234567890003 95
1234567890002 77
1234567890004 85
4
1234567890013 65
1234567890011 25
1234567890014 100
1234567890012 85

Sample Output

9
1234567890005 1 1 1
1234567890014 1 2 1
1234567890001 3 1 2
1234567890003 3 1 2
1234567890004 5 1 4
1234567890012 5 2 2
1234567890002 7 1 5
1234567890013 8 2 3
1234567890011 9 2 4

思路: 题意大致是先输入考场数目,然后输入每个考场学生数目,最后输入学生id与成绩。
输出总人数,每个学生的id,总排名,考场排名,
按照总排名输出。
代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>

using namespace std;

struct node {
    string id;
    int final_rank, location_number, local_rank;
    int score;
}a[30003];

bool cmp(node x, node y)
{
    return x.score == y.score ? x.id < y.id : x.score > y.score;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    int n;
    int num = 0;
    cin >> n;

    for (int i = 1; i <= n; ++i)
    {
        int k;
        cin >> k;
        for (int j = 1; j <= k; ++j)
        {
            cin >> a[num].id;
            cin >> a[num].score;
            a[num].location_number = i;
            num++;
        }
        sort(a + num - k, a + num, cmp);
        a[num - k].local_rank = 1;
        for (int j = 1; j < k; ++j)
        {
            if (a[num - k + j].score == a[num - k + j - 1].score)
                a[num - k + j].local_rank = a[num - k + j - 1].local_rank;
            else
                a[num - k + j].local_rank = j + 1;
        }
    }

    sort(a, a + num, cmp);

    a[0].final_rank = 1;

    for (int i = 1; i < num; ++i)
    {
        if (a[i].score == a[i - 1].score)
            a[i].final_rank = a[i - 1].final_rank;
        else
            a[i].final_rank = i + 1;
    }
	
	cout << num << endl;

    for (int i = 0; i < num; ++i)
    {
        cout << a[i].id << " " << a[i].final_rank << " " << a[i].location_number << " " << a[i].local_rank << endl;
    }

    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值