PAT-A1025-PAT Ranking

PAT Ranking - 题目链接


题目描述

 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.

输入描述

 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.

输出描述

 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.


示例:

输入

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

输出

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


解题思路

  首先使用结构数组存储输入的记录(学号,分数,考场,排名)
  之后在存数据的同时,对同一考场的数据进行排序,记录在排名里面
  当各个考场的数据写完后,再按照全部的数据进行排序(规则是分数是第一优先级,编号是第二优先级)
 设置一个rank初始值令它为1,遇见分数相同的就排名相同,否则排名的值就是位置

参考代码

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

struct Stu {
    char id[20];
    int grade;
    int local_area;
    int local_rank;
} Stu[50000];
// 根据比较的优先级cmp函数
bool cmp(struct Stu a, struct Stu b) {
    if (a.grade != b.grade) { return a.grade > b.grade; }
    else { return strcmp(a.id, b.id) < 0; }
}

int main() {
    int n, num = 0;
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) {
        int m;
        scanf("%d", &m);
        for (int j = 0; j < m; ++j) {
            scanf("%s %d", Stu[num].id, &Stu[num].grade);
            Stu[num].local_area = i;
            ++num;
        }
        //对同一考场的人进行排序
        sort(Stu + num - m, Stu + num, cmp);
        Stu[num - m].local_rank = 1;
        for (int j = num - m + 1; j < num; j++) {
            if (Stu[j].grade == Stu[j - 1].grade) {
                Stu[j].local_rank = Stu[j - 1].local_rank;
            } else {
                Stu[j].local_rank = j + 1 - (num - m);
            }
        }
    }
    printf("%d\n", num);
    sort(Stu, Stu + num, cmp);
    //不同考场的人进行的排序
    int r = 1;
    for (int i = 0; i < num; ++i) {
        if (i>0 && Stu[i].grade != Stu[i - 1].grade) {
            r = i + 1;
        }
        printf("%s ", Stu[i].id);
        printf("%d %d %d\n", r, Stu[i].local_area, Stu[i].local_rank);
    }

    return 0;
}
//自己第一次写的 不知错哪里 上面按照算法笔记的思路来了一遍
//自己思路虽然清晰 但是实现起来好复杂 算法笔记的比较清晰
/*#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

struct Student {
    char id[20];
    int grade;
    int allRank;
    int singRank;
    int area;
} Student[30010];

int num = 0;
int groupnum[30010] = {0};
int singlegroup[30010] = {0};

bool cmp(struct Student a, struct Student b) {
    if (a.grade != b.grade) { return a.grade > b.grade; }
    else { return strcmp(a.id, b.id) < 0; }
}

bool cmp_1(struct Student a, struct Student b) {
    if (a.area != b.area) {
        return a.area < b.area;
    } else {
        return a.grade > b.grade;
    }
}

int main() {
    int n;
    int allnum = 0;
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
        int m = 0;
        scanf("%d", &m);
        singlegroup[i + 1] = m;
        allnum += m;
        groupnum[i + 1] = allnum;
        for (int j = 0; j < m; ++j) {
            scanf("%s", Student[num].id);
            scanf("%d", &Student[num].grade);
            Student[num++].area = i + 1;
        }
    }
    sort(Student, Student + allnum, cmp_1);
    for (int i = 1; i <= n; ++i) {
        int ranknum = 1;
        Student[groupnum[i - 1]].singRank = ranknum;
        for (int j = groupnum[i - 1] + 1; j < groupnum[i]; ++j) {
            if (Student[j].grade != Student[j - 1].grade) {
                Student[j].singRank = ++ranknum;
            } else {
                Student[j].singRank = ranknum;
                ++ranknum;
            }
        }
    }
    sort(Student, Student + allnum, cmp);
    Student[0].allRank = 1;
    int ranknum = 1;
    for (int i = 1; i < allnum; ++i) {
        if (Student[i].grade != Student[i - 1].grade) {
            Student[i].allRank = ++ranknum;
        } else {
            Student[i].allRank = ranknum;
            ++ranknum;
        }
    }
    printf("%d\n", allnum);
    for (int i = 0; i < allnum; ++i) {
        printf("%s %d %d %d\n", Student[i].id, Student[i].allRank, Student[i].area, Student[i].singRank);
    }
    return 0;
}
*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值