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

生词

英文 解释
simultaneously 同时
testee 受试人
registration 登记,注册

题目大意:

有n个考场,每个考场有若干数量的学生,给出每个考场中考生的编号和分数,要求算排名,输出所有考生的编号、排名、考场号、考场内排名

分析:

先按照考场内排名 然后赋值给总数组fin,然后总排名,最后输出。注意相同的分数情况下按照学号的从小到大排列,但是他们的排名应该是一样的数字~

原文链接:https://blog.csdn.net/liuchuo/article/details/52188092

疑惑点

不知道为什么这样写无法输出结构体数组的值??
2021-11-16 21:47:45 星期二 更新
感谢@纪流汐夜让我明白了为啥出问题了!
因为while(k--)的时候k的值最后会变成0。然后是进不去for(int j=1;j<k;j++)循环的Orz

#include <bits/stdc++.h>

using namespace std;
struct testee
{
    long long int id;
    int score,finalrk,local,localrk;
}t[10000];
bool cmp(testee a,testee b)
{
    return a.score>b.score;
}
int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("1.txt", "r", stdin);
#endif
    int n,k,cnt=0;
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>k;
        while(k--){
            cin>>t[cnt].id>>t[cnt].score;
            t[cnt].local=i;
            //cout<<t[cnt].local<<" "<<t[cnt].score<<endl;
            cnt++;

        }/*
        sort(t,t+k,cmp);
        t[0].localrk=1;
        for(int j=1;j<k;j++){
            t[j].localrk=j+1;
            if(t[j].localrk==t[j-1].localrk)
                t[j].localrk=t[j-1].localrk;
        }*/

    }
	//这里无法输出t数组前k个变量的值?疑惑
    for(int i=0;i<k;i++)
        cout<<t[i].local<<" "<<t[i].score<<endl;
	//但是其实t数组前k个变量都是有值的啊,迷惑??
    cout<<t[4].local<<" "<<t[4].score<<endl;
    return 0;
}

image

题解

注意点

  1. fin[i].finalrk=(fin[i].score==fin[i-1].score)?fin[i-1].finalrk:i+1;比较的分数值,不是finalrk;
  2. %013lld输出的id要控制13位。
    这题这么简单,不知道为啥卡了半天Orz服了,然后那个困惑点还不知道是怎么回事??
#include <bits/stdc++.h>

using namespace std;
struct testee
{
    long long int id;
    int score,finalrk,local,localrk;
};
bool cmp(testee a,testee b)
{
    return a.score!=b.score?a.score>b.score:a.id<b.id;
}
int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("1.txt", "r", stdin);
#endif
    int n,k,cnt=0;
    cin>>n;
    vector<testee> fin;
    for(int i=1;i<=n;i++){
        cin>>k;
        vector<testee> v(k);
        for(int j=0;j<k;j++){
            scanf("%lld %d",&v[j].id,&v[j].score);
            v[j].local=i;

        }
        sort(v.begin(),v.end(),cmp);
        v[0].localrk=1;
        fin.push_back(v[0]);
        for(int j=1;j<k;j++){
            v[j].localrk=j+1;
            if(v[j].score==v[j-1].score)
                v[j].localrk=v[j-1].localrk;
            fin.push_back(v[j]);
        }
    }
    sort(fin.begin(),fin.end(),cmp);
    fin[0].finalrk=1;
    for(int i=1;i<fin.size();i++){
        fin[i].finalrk=(fin[i].score==fin[i-1].score)?fin[i-1].finalrk:i+1;
    }
    printf("%d
",fin.size());
    for(int i=0;i<fin.size();i++){
        printf("%013lld %d %d %d
",fin[i].id,fin[i].finalrk,fin[i].local,fin[i].localrk);
    }
    return 0;
}

本文来自博客园,作者:勇往直前的力量,转载请注明原文链接:https://www.cnblogs.com/moonlight1999/p/15530693.html

原文地址:https://www.cnblogs.com/moonlight1999/p/15530693.html