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

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

思路:
  1、这个题主要是计算两个排名——局部排名和最终排名。
  2、每当输入完一个局部数集后,对它按题目要求排序,也就分数高的靠前,分数相同id小的靠前排。计算排名的时候要注意,分数相同的
排名一样。比如100,100 80,70对应的排名是1、1、3、4没有第二名。局部排名计算完后,计算最终排名。
  3、最后一个测试样例里面的id前面几位数字是0,所以输出的时候要注意右对齐补零。
  4、
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>

using namespace std;

struct Stu
{
    int loc;
    long long id;
    int score;
    //int rank;
    int frank;
    //int loc;
    int lrank;

};

bool cmp(Stu&A,Stu&B)
{
   return A.score==B.score?A.id<B.id:A.score>B.score;
}

int main()
{
    int n;
    cin>>n;
    vector<Stu> tests[n],test;
    for(int i=0; i<n; i++)
    {
        int m=0;
        cin>>m;
        Stu temp;
        for(int j=0; j<m; j++)
        {
            temp.loc=i+1;
            cin>>temp.id>>temp.score;
            tests[i].push_back(temp);
            //test.push_back(temp);
        }
        sort(tests[i].begin(),tests[i].end(),cmp);
        tests[i][0].lrank=1;
        test.push_back(tests[i][0]);
        //cout<<tests[i][0].score<<endl;
        for(int j=1; j<tests[i].size(); j++)
        {
            //cout<<tests[i][j].score<<endl;
            if(tests[i][j-1].score==tests[i][j].score)
                tests[i][j].lrank=tests[i][j-1].lrank;
            else
                tests[i][j].lrank=j+1;
            test.push_back(tests[i][j]);
        }

    }
    sort(test.begin(),test.end(),cmp);
   // int t=0;
    cout<<test.size()<<endl;
    //vector<Output> output(test.size());
    test[0].frank=1;
    for(int i=1; i<test.size(); i++)
    {
        if(test[i-1].score==test[i].score)
            test[i].frank=test[i-1].frank;
        else
            test[i].frank=i+1;
    }
    for(int i=0; i<test.size(); i++)
    {
        printf("%013lld ",test[i].id);
        cout<<test[i].frank<<" "<<test[i].loc<<" "<<test[i].lrank<<endl;
    }
    return 0;
}
长整型数字printf输出格式是%lld
  
原文地址:https://www.cnblogs.com/zhanghaijie/p/10295400.html