PAT A1025.PAT Ranking (25 分)

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 (≤), the number of test locations. Then N ranklists follow, each starts with a line containing a positive integer K (≤), 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

题意:N个考场,每个考场若干个考生,给出考生号和成绩,要求按总排名输出,同时输出考场内部排名。

分析:创建结构体存储考生信息。每读完一个考场内所有的考生信息后,进行考场内部排名。全部读完之后对所有考生进行统一排名。

#include<bits/stdc++.h>
using namespace std;
struct student{
    char id[15];
    int scores;
    int position_num;//考场号
    int local_rank;//考城内排名
}stu[30010];

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

int main(){
    int N, all_num = 0;
    cin >> N;
    int M;
    for (int i = 1; i <= N; i++){

        scanf("%d", &M);
        for (int j = 0; j < M; j++){
            scanf("%s %d", stu[all_num].id, &stu[all_num].scores);
            stu[all_num].position_num = i;
            all_num ++;
        }

        sort(stu + all_num - M, stu + all_num, cmp);
        stu[all_num - M].local_rank = 1;//场内排名为1
        for (int k = all_num - M + 1; k < all_num; k++){
            if (stu[k].scores == stu[k - 1].scores){
                stu[k].local_rank = stu[k - 1].local_rank;
            }else{
                stu[k].local_rank = k + 1 - all_num + M;
            }
        }
    }
    printf("%d\n", all_num);

    sort(stu, stu + all_num, cmp);
    int r = 1;
    //printf("%s %d 1 %d", stu[0].id, stu[0].position_num, stu[0].local_rank);
    for (int k = 0; k < all_num; k++){
        if ( k > 0 && stu[k].scores != stu[k - 1].scores)
            r = k + 1;
        printf("%s %d %d %d\n", stu[k].id, r, stu[k].position_num, stu[k].local_rank);
    }

    return 0;
}

 

posted @ 2019-07-01 21:58  夜半歌声断  阅读(174)  评论(0编辑  收藏  举报