pat1025PAT Ranking (25)

56 篇文章 0 订阅

题意分析:

(1)给出多个考点的考试人数以及考生考号和成绩,求出所有参加考试的人最终排名、考场编号、本考场排名

(2)水题,考察排序,排名的规则按照以前的做法:按分数由高到低,同分同名次,不同分,按其在序列中的位置算排名

可能坑点:

#include <iostream>
#include <algorithm>
using namespace std;

struct testee
{
    string ID;
    int location;
    int score;
    int finalRank;
    int localRank;
};
testee stu[30001];

bool cmp(testee a,testee b)
{
    if(a.score!=b.score)return a.score>b.score;
    else return a.ID<b.ID;
}

void getLocalRank(int start,int end)
{
    stu[start].localRank=1;
    int currentScore=stu[start].score;
    int currentRank=stu[start].localRank;
    for(int i=start;i<end;i++)
    {
        if(stu[i].score==currentScore)stu[i].localRank=currentRank;
        else
        {
            stu[i].localRank=i+1-start;
            currentScore=stu[i].score;
            currentRank=stu[i].localRank;
        }
    }
}

void getFinalRank(int start,int end)
{
    stu[start].finalRank=1;
    int currentScore=stu[start].score;
    int currentRank=stu[start].finalRank;
    for(int i=start;i<end;i++)
    {
        if(stu[i].score==currentScore)stu[i].finalRank=currentRank;
        else
        {
            stu[i].finalRank=i+1;
            currentScore=stu[i].score;
            currentRank=stu[i].finalRank;
        }
    }
}

int main()
{
    int N,K,i=1,k=0;
    cin>>N;
    while(i<=N)
    {
        cin>>K;
        int j=0;
        while(j<K)
        {
            cin>>stu[k].ID>>stu[k].score;
            stu[k].location=i;
            k++;
            j++;
        }
        sort(&stu[k-K],&stu[k],cmp);
        getLocalRank(k-K,k);
        i++;
    }
    sort(&stu[0],&stu[k],cmp);
    getFinalRank(0,k);
    cout<<k<<endl;
    for(int i=0;i<k;i++)
    {
        cout<<stu[i].ID<<" "<<stu[i].finalRank<<" "<<stu[i].location<<" "<<stu[i].localRank<<endl;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值