基础实验7-2.4 PAT排名汇总 (25分)

计算机程序设计能力考试(Programming Ability Test,简称PAT)旨在通过统一组织的在线考试及自动评测方法客观地评判考生的算法设计与程序设计实现能力,科学的评价计算机程序设计人才,为企业选拔人才提供参考标准(网址http://www.patest.cn)。

每次考试会在若干个不同的考点同时举行,每个考点用局域网,产生本考点的成绩。考试结束后,各个考点的成绩将即刻汇总成一张总的排名表。

现在就请你写一个程序自动归并各个考点的成绩并生成总排名表。

输入格式:

输入的第一行给出一个正整数N(≤100),代表考点总数。随后给出N个考点的成绩,格式为:首先一行给出正整数K(≤300),代表该考点的考生总数;随后K行,每行给出1个考生的信息,包括考号(由13位整数字组成)和得分(为[0,100]区间内的整数),中间用空格分隔。

输出格式:

首先在第一行里输出考生总数。随后输出汇总的排名表,每个考生的信息占一行,顺序为:考号、最终排名、考点编号、在该考点的排名。其中考点按输入给出的顺序从1到N编号。考生的输出须按最终排名的非递减顺序输出,获得相同分数的考生应有相同名次,并按考号的递增顺序输出。

输入样例:

2
5
1234567890001 95
1234567890005 100
1234567890003 95
1234567890002 77
1234567890004 85
4
1234567890013 65
1234567890011 25
1234567890014 100
1234567890012 85
 

输出样例:

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 <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#define Max 1001
using namespace std;

struct pat
{
    char num[14];//记录编号
    int sco,loca,locaran,ran;//sco记录成绩loca记录所属于location locaran记录所在location的排名 ran记录总排名
}tot[30000];//记录总的数据
int last[101];//所在location中上一个元素的下标
int ind[101];//每个location已经输出的元素个数
int no = 0;//总数据的下标
int n,k;
bool cmp(pat a,pat b)
{
    if(a.sco == b.sco)return strcmp(a.num,b.num) < 0;
    return a.sco > b.sco;
}
int main()
{
    cin>>n;
    for(int i = 1;i <= n;i ++)
    {
        cin>>k;
        last[i] = -1;//初始为-1,一开始不存在所在location 的上一个元素下标  i就是location编号
        for(int j = 0;j < k;j ++)
        {
            cin>>tot[no + j].num>>tot[no + j].sco;
            tot[no + j].loca = i;///记录location编号
        }
        no += k;//更新总下标
    }
    sort(tot,tot+no,cmp);
    cout<<no<<endl;
    cout<<tot[0].num<<' '<<1<<' '<<tot[0].loca<<' '<<++ ind[tot[0].loca]<<endl;
    tot[0].locaran = 1;
    tot[0].ran = 1;
    last[tot[0].loca] = 0;

    for(int i = 1;i < no;i ++)
    {
        if(tot[i].sco == tot[i - 1].sco)//跟前一个元素成绩相同,总排名就相同
        {
            tot[i].ran = tot[i - 1].ran;
        }
        else tot[i].ran = i + 1;//否则等于当前i + 1
        if(last[tot[i].loca] >= 0 && tot[i].sco == tot[last[tot[i].loca]].sco)//跟同一个location上一个元素成绩相同 则排名相同
        {
            tot[i].locaran = tot[last[tot[i].loca]].locaran;
            ++ ind[tot[i].loca];///当前元素所在location元素个数加1
        }
        else tot[i].locaran = ++ ind[tot[i].loca];//否则就是所在location总的元素个数
        last[tot[i].loca] = i;//更新last
        cout<<tot[i].num<<' '<<tot[i].ran<<' '<<tot[i].loca<<' '<<tot[i].locaran<<endl;
    }
}
原文地址:https://www.cnblogs.com/8023spz/p/12303493.html