PAT_A1025 | PAT Ranking

1025 PAT Ranking (25point(s))

Programming Ability Test (PAT) is organized by the College of Computer Science and Technology of Zhejiang University. Each test is supposed to run simultaneously in several places, and the ranklists will be merged immediately after the test. Now it is your job to write a program to correctly merge all the ranklists and generate the final rank.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive number N (≤100), the number of test locations. Then N ranklists follow, each starts with a line containing a positive integer K (≤300), the number of testees, and then K lines containing the registration number (a 13-digit number) and the total score of each testee. All the numbers in a line are separated by a space.

Output Specification:

For each test case, first print in one line the total number of testees. Then print the final ranklist in the following format:

registration_number final_rank location_number local_rank

 

The locations are numbered from 1 to N. The output must be sorted in nondecreasing order of the final ranks. The testees with the same score must have the same rank, and the output must be sorted in nondecreasing order of their registration numbers.

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

每个考生的信息比较多,应该使用结构体储存。所有考生的信息就总和在结构体数组中。

struct node {
    char registration_number[15];
    int score;
    int final_rank;
    int location_number;
    int local_rank;
} stu[MAX];

涉及到的排序,可以使用c++"algorithm"库中的sort函数,具体如何使用可以看看这篇文章:排序算法 | sort函数的使用

本题要求以成绩从高到低排,成绩相同时,注册号小的排在前。cmp函数应该这样写:

bool cmp(struct node a, struct node b) {
    /* 分数不同的情况*/
    if (a.score != b.score)
        return a.score > b.score;   //按照分数增序排列
    /* 分数相同的情况*/
    return strcmp(a.registration_number, b.registration_number) < 0; //按照注册号升序排列
}

步骤: 

1、按照地区录入考生信息,同时录入地区号。一个地区的考生全都录入完毕后,对这个地区内的考生进行排序,然后给该地区内考生排名,排名结果储存在local_rank变量中。

2、对全体考生进行排序,然后根据排序结果给所有考生排名,排名结果储存在final_rank变量中。

3、按照排序结果依次按要求输出。

 

注意点:

输入和sort函数传入地址排序的时候记得把地址对应正确。我这里采用一个count变量来记录考生数,在一个地区录入且排名完毕后再更新,那么在某地区的考生录入或排序时,对应的数组下标都要从count开始。

 

完整代码:

//
// Created by LittleCat on 2020/2/11.
//
#include <cstdio>
#include <cstring>
#include <algorithm>

#define MAX 30050

using namespace std;

struct node {
    char registration_number[15];
    int score;
    int final_rank;
    int location_number;
    int local_rank;
} stu[MAX];

bool cmp(struct node a, struct node b) {
    /* 分数不同的情况*/
    if (a.score != b.score)
        return a.score > b.score;   //按照分数增序排列
    /* 分数相同的情况*/
    return strcmp(a.registration_number, b.registration_number) < 0; //按照注册号升序排列
}

int main() {

    int n, count = 0;
    scanf("%d", &n);

    for (int location_number = 1; location_number <= n; location_number++) {

        int num;  //该地区人数
        scanf("%d", &num);

        for (int j = 0; j < num; j++) {
            stu[count + j].location_number = location_number;
            scanf("%s %d", stu[count + j].registration_number, &stu[count + j].score);
        }

        sort(stu + count, stu + count + num, cmp); //对该地区内排序

        stu[count].local_rank = 1; //第一名
        //对剩下num - 1人排名
        for (int j = 1; j < num; j++) {
            //和上一位分数相同
            if (stu[count + j].score == stu[count + j - 1].score)
                stu[count + j].local_rank = stu[count + j - 1].local_rank;
            else
                stu[count + j].local_rank = j + 1;
        }
        count += num;  //总人数增加
    }

    sort(stu, stu + count, cmp);  //对全部人排序
    stu[0].final_rank = 1; //第一名
    //对剩下count - 1人排名
    for (int i = 1; i < count; 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", count);
    for (int i = 0; i < count; i++)
        printf("%s %d %d %d\n", stu[i].registration_number,
               stu[i].final_rank, stu[i].location_number, stu[i].local_rank);

}

 

 

有一个疑问:

我最开始结构体中是用longlong型变量存储注册号的,但是这样写出的代码最后一个用例不能通过,之后改成字符串储存才过的,有大佬知道为什么吗?



end 

欢迎关注个人公众号 鸡翅编程 ”,这里是认真且乖巧的码农一枚。

---- 做最乖巧的博客er,做最扎实的程序员 ----

旨在用心写好每一篇文章,平常会把笔记汇总成推送更新~

在这里插入图片描述

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值