PAT--1025 PAT Ranking(排序)

参考博客: https://blog.csdn.net/Invokar/article/details/80722602

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

struct Node
{
    string stnum;
    int score, frank, locnum, lrank;
};

Node stud[30001];

bool cmp(Node s1, Node s2)
{
    if(s1.score == s2.score)
        return s1.stnum < s2.stnum;    //分数相同,按照学号升序
    else
        return s1.score > s2.score;     //按照分数降序
}

int main()
{
    int n, k;
    cin >> n;
    int first = 0, idx = 0;
    for(int i = 1; i <= n; i++)
    {
        cin >> k;
        first = idx;    //记录每一组数据的开始下标
        for(int j = 0; j < k; j++)
        {
            cin >> stud[idx].stnum >> stud[idx].score;
            stud[idx].locnum = i;
            idx++;
        }
        //局部排序
        sort(stud+first, stud+first+k, cmp);
        for(int j = first; j < idx; j++)
        {
            if(j > first && stud[j].score == stud[j-1].score)
                stud[j].lrank = stud[j-1].lrank;
            else
                stud[j].lrank = j-first+1;
        }
    }
    //整体排序
    sort(stud, stud+idx, cmp);
    for(int j = 0; j < idx; j++)
        if(j > 0 && stud[j].score == stud[j-1].score)
            stud[j].frank = stud[j-1].frank;
        else
            stud[j].frank = j+1;

    cout << idx << endl;
    for(int j = 0; j < idx; j++)
        cout << stud[j].stnum << " " << stud[j].frank << " " << stud[j].locnum << " " << stud[j].lrank << endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/mch2869253130/article/details/88096165