PAT甲级.1025. PAT Ranking (25)

来源:互联网 发布:微信小店数据推送 编辑:程序博客网 时间:2024/05/16 09:13

题目

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

PAT链接

思路

  1. 用一个struct 来存储id, 分数,所在考场,所在考场的排名,总排名
struct Student{    char id[15];    int score;    int location;    int localRank;  //所在考场的排名    int globalRank; //总排名}stu[30010];
  1. cmp 函数来指定排序规则
bool cmp(Student a, Student b){    if (a.score != b.score)  return a.score > b.score;    else return strcmp(a.id, b.id) < 0; //一开始写成else return strcmp(a.id, b.id);报错}
  1. 分别对每一个考场排序,记录排名。再对总考场排序,记录总排名

代码

/*** @tag     PAT_A_1025* @authors R11happy (xushuai100@126.com)* @date    2017-2-6 22:00 - 22:58* @version 1.0* @Language C++* @Ranking  780/1705* @function null*/#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>using namespace std;struct Student{    char id[15];    int score;    int location;    int localRank;  //所在考场的排名    int globalRank; //总排名}stu[30010];bool cmp(Student a, Student b){    if (a.score != b.score)  return a.score > b.score;    else return strcmp(a.id, b.id) < 0; //一开始写成else return strcmp(a.id, b.id);报错}int main(int argc, char const *argv[]){    int indexLocation;    int N, K, index = 0;    int numOfTestee = 0;    scanf("%d", &N);    for (indexLocation = 1; indexLocation <= N; indexLocation++)    {        scanf("%d", &K);        for (int i = 0; i<K; i++)        {            scanf("%s %d", stu[numOfTestee].id, &stu[numOfTestee].score);            stu[numOfTestee].location = indexLocation;            numOfTestee++;        }        // 将该考场排序        sort(stu + numOfTestee - K, stu + numOfTestee, cmp);        stu[numOfTestee - K].localRank = 1;        for (int i = numOfTestee - K + 1; i < numOfTestee; ++i)        {            if (stu[i].score == stu[i - 1].score)  stu[i].localRank = stu[i - 1].localRank;            else    stu[i].localRank = i + K -numOfTestee+1;    //当前所在考场次序下标加1        }    }    // 全体排序    sort(stu, stu + numOfTestee, cmp);    stu[0].globalRank = 1;    for (int i = 1; i<numOfTestee; i++)    {        if (stu[i].score == stu[i - 1].score)  stu[i].globalRank = stu[i - 1].globalRank;        else    stu[i].globalRank = i + 1;    }    printf("%d\n", numOfTestee);    for (int i = 0; i < numOfTestee; ++i)    {        printf("%s %d %d %d\n", stu[i].id, stu[i].globalRank, stu[i].location, stu[i].localRank);    }    return 0;}

收获

  • cmp函数的编写,int类型直接return a.score > b.score ,char[]类型用strcmpreturn strcmp(a.id, b.id) < 0
  • 排名的实现
    当要求分数不同的排名不同,分数相同的排名相同但占用一个排位的情况
    1. 在结构体定义的时候将排名这一项加到结构体中
    int localRank;  //所在考场的排名    int globalRank; //总排名
  1. 先将数组第一个个体的排名记为1,然后遍历剩余个体,如果当前个体的分数等于上一个个体的分数,那么当前个体的排名等于上一个个体的排名,否则,当前个体的排名等于数组下标加1
    for (int i = 1; i<numOfTestee; i++)    {        if (stu[i].score == stu[i - 1].score)  stu[i].globalRank = stu[i - 1].globalRank;        else    stu[i].globalRank = i + 1;    }
0 0