PAT.PAT Ranking(结构体排序,注意细节)

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.

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

写了这个题发现自己是真的弱,从一开始的思路混乱乱写一通,最后觉得没问题交上去2个test是wa的,于是又做了一遍,
才发现自己是真的垃圾,具体什么地方错了,下面代码会有标注。。。。。。

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

const int maxn = 100 + 5, inf = 0x3f3f3f3f;

struct Stu {
    string id;
    int score, final_rank, location_number, local_rank;
};

vector <Stu> ans;
vector <Stu> stu[maxn];

bool cmp1(Stu a, Stu b) {
    return a.score > b.score;
}

bool cmp2(Stu a, Stu b) {
    if(a.score == b.score) return a.id < b.id;
    return a.score > b.score;
}

int cnt;

int main() {
    int n, k, score;
    string id;
    Stu temp;
    while(cin >> n) {
        for(int i = 1; i <= n; i ++) {
            cin >> k;
            for(int j = 1; j <= k; j ++) {
                cin >> id >> score;
                stu[i].push_back(Stu{id, score, inf, i, inf});
            }
        }
        for(int i = 1; i <= n; i ++) {
            sort(stu[i].begin(), stu[i].end(), cmp1);
            temp = stu[i][0];
            cnt = 1;
            stu[i][0].local_rank = 1;
            ans.push_back(Stu{temp.id, temp.score, temp.final_rank, temp.location_number, cnt});
            for(int j = 1; j < stu[i].size(); j ++) {
                cnt ++;
                temp = stu[i][j];
                if(temp.score == stu[i][j - 1].score) ans.push_back(Stu{temp.id, temp.score, temp.final_rank, temp.location_number, stu[i][j].local_rank = stu[i][j - 1].local_rank});
                else ans.push_back(Stu{temp.id, temp.score, temp.final_rank, temp.location_number, stu[i][j].local_rank = cnt}); // stu[i][j].local_rank = cnt 这里,如果这里不修改值,那么遇到连续的排名相同会导致后面的同学无排名,,,,
            }
        }
        sort(ans.begin(), ans.end(), cmp2);
        ans[0].final_rank = 1;
        cnt = 1;
        for(int i = 1; i < ans.size(); i ++) {
            cnt ++;
            if(ans[i].score == ans[i - 1].score) ans[i].final_rank = ans[i - 1].final_rank;
            else ans[i].final_rank = cnt;
        }
        cout << ans.size() << endl;
        for(int i = 0; i < ans.size(); i ++) {
            temp = ans[i];
            cout << temp.id << ' ' << temp.final_rank << ' ' << temp.location_number << ' ' << temp.local_rank << endl;
        }
    }
    return 0;
}
 

猜你喜欢

转载自www.cnblogs.com/bianjunting/p/12578685.html
今日推荐