PAT(甲级)1025.PAT Ranking(25)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ASJBFJSB/article/details/89065329

PAT 1025.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.

输入格式:
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

题目分析:分组排名,整体排名。

注意:由于不知道题目会输入多少个组,每一个组是一个vector对象,因此可以开辟110个vector数组。相当于一个二维数组。首先进行各组的组内排名,都排完之后将所有组同学合并到同一个vector中,进行最后一次排名,然后一次输出即可。

AC代码:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

const int maxn = 110;

struct people{
    string name;
    int score;
};

struct stu{
    string name;
    int group;
    int score;
    int groupRank;

    stu(string name, int group, int score, int groupRank){
        this->name = name;
        this->group = group;
        this->score = score;
        this->groupRank = groupRank;
    }
};

vector<people> arr[maxn];
vector<stu> v;

bool cmp(people a, people b){
    if(a.score != b.score)return a.score > b.score;
    else return a.name < b.name;
}

bool cmp1(stu a, stu b){
    if(a.score != b.score)return a.score > b.score;
    else return a.name < b.name;
}


int main(){
    int n, m;
    scanf("%d", &n);
    for(int i=0; i<n; ++i){
        scanf("%d", &m);
        people p;
        string name;
        int score;
        for(int j=0; j<m; ++j){
            cin>>name;
            scanf("%d", &score);
            p.name = name;
            p.score = score;
            arr[i].push_back(p);
        }
        sort(arr[i].begin(), arr[i].end(), cmp);
        int rank = 0, pre=-1;
        for(int k=0; k<arr[i].size(); ++k){
            if(arr[i][k].score != pre)rank = k+1;
            pre = arr[i][k].score;
            v.push_back(stu(arr[i][k].name, i+1, arr[i][k].score, rank));
        }
    }
    int cnt = 0;
    for(int i=0; i<n; ++i){
        cnt += arr[i].size();
    }
    printf("%d\n", cnt);
    sort(v.begin(), v.end(), cmp1);
    int rank=0,pre=-1;
    for(int i=0; i<v.size(); ++i){
        if(v[i].score!=pre)rank = i+1;
        pre = v[i].score;
        printf("%s %d %d %d\n", v[i].name.c_str(), rank, v[i].group, v[i].groupRank);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/ASJBFJSB/article/details/89065329
今日推荐