《算法笔记》题目PAT A1025-PAT Ranking

题目:

 

 题目大意:

输入:考场数、一个考场的选手数、选手的编号和分数

输出:选手的总人数、选手的编号、选手的总排名、选手所在的考场号、选手所在的当地排名(分数相同的排名相同)

思路:定义一个结构体,输入时用sort函数对结构体进行排序,确定选手所在考场的排名;输出时,确定选手的总排名

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

struct Student{
    long long id;
    int score;
    int local_num;//考场号
    int local_rank;//地方排名
};
struct Student student[30020];
bool cmp(Student a,Student b)//比较方式,相同分数的按字典序排名
{
    if(a.score!=b.score) return a.score > b.score;
    else return a.id<b.id;
}

int main()
{
    int n,k,num=0;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&k);
        for(int j=0;j<k;j++)
        {
            scanf("%lld%d",&student[num].id,&student[num].score);
            student[num].local_num=i;
            num++;
        }
        sort(student+num-k,student+num,cmp);//对每个考场的分数进行排序
        student[num-k].local_rank=1;
        for(int j=1;j<k;j++)
        {
            if(student[num-k+j].score==student[num-k+j-1].score)
                student[num-k+j].local_rank=student[num-k+j-1].local_rank;
            else
                student[num-k+j].local_rank=j+1;
        }

    }
    printf("%d
",num);
    sort(student,student+num,cmp);
    int r=1;
    for(int i=0;i<num;i++)
    {
        if(i>0&&student[i].score!=student[i-1].score)
            r=i+1;
        printf("%lld %d %d %d
",student[i].id,r,student[i].local_num,student[i].local_rank);
    }
    return 0;
}

 最后一个点就是过不去≧ ﹏ ≦

作者:inss!w!
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
原文地址:https://www.cnblogs.com/Hfolsvh/p/14355371.html