PAT1025 PAT Ranking

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

 

 

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

struct STUDENT
{
    string regnum;
    int score;
    int location;
    int totalrank;
    int locationrank;
    STUDENT()
    {
        score = 0;
        location = 0;
        totalrank = 0;
        locationrank = 0;
    }
};

bool greatermark(const STUDENT& s1,const STUDENT& s2)   
{   
    return   s1.score   >   s2.score;   
}


bool lessregnum(const STUDENT& s1,const STUDENT& s2)   
{   
    return   s1.regnum   <   s2.regnum;   
}

bool sortByScore(STUDENT a,STUDENT b)  
{  
    if(a.score != b.score)  
        return  a.score > b.score;  

    return a.regnum < b.regnum;  
} 


int main()
{
    int N;
    cin>>N;
    int i = 0;
    vector<STUDENT> VArray[100];
    while(N--)
    {
        int K;
        cin>>K;
        vector<STUDENT> V;
        while(K--)
        {
            STUDENT stu;
            cin>>stu.regnum>>stu.score;
            stu.location = i + 1;
            V.push_back(stu);
        }
        sort(V.begin(), V.end(), sortByScore);


        int forword = -1;
        int irank = 0;
        int realrank = 0;
        vector<STUDENT>::iterator iter = V.begin();
        while(iter != V.end())
        {
            ++realrank;
            if (forword != (*iter).score)
            {
                irank = realrank;
                (*iter).locationrank = realrank;
                forword = (*iter).score;
            }
            else
            {
                (*iter).locationrank = irank;
            }
            iter++;
        }
        VArray[i] = V;
        i++;
    }

    vector<STUDENT> VSum;
    for (int i = 0; i < 100; i++)
    {
        VSum.insert(VSum.end(),VArray[i].begin(),VArray[i].end());
    }
    sort(VSum.begin(), VSum.end(), sortByScore);

    int forword = -1;
    int irank = 0;
    int realrank = 0;
    vector<STUDENT>::iterator iter = VSum.begin();
    while(iter != VSum.end())
    {
        ++realrank;
        if (forword != (*iter).score)
        {
            irank = realrank;
            (*iter).totalrank = realrank;
            forword = (*iter).score;
        }
        else
        {
            (*iter).totalrank = irank;
        }
        iter++;
    }

    cout<<VSum.size();
    cout.setf(ios::fixed);
    for (int i = 0; i < VSum.size(); i++)
    {
        cout<<endl;
        cout<<VSum[i].regnum<<" "<<VSum[i].totalrank<<" "<<VSum[i].location<<" "<<VSum[i].locationrank;
    }
}
 

你可能感兴趣的:(rank)