PAT A1141 PAT Ranking of Institutions (25 分) 排序

    题目大意:给出N名考生的考试级别和考场信息,统计每个考场的考生获得的总分以及每个考场的总人数,并按要求排序给出考场的排名。

    排序题,按要求来即可。只是注意题目中说按TWS排序,这里的TWS是取整后的值,否则和参考示例对不上。

AC代码:

#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <map>
using namespace std;

struct institution
{
    string id;
    double tws;
    int ns;
    institution():tws(0), ns(0){};
    bool operator<(const institution& other)
    {
        if(this->tws != other.tws) return this->tws > other.tws;
        else if(this->ns != other.ns) return this->ns < other.ns;
        else return this->id < other.id;
    }
};
map<string, institution> mp;
int main()
{
    int N;
    scanf("%d", &N);
    for (int i = 0; i < N; ++i)
    {
        char student[10], id[10];
        double score;
        scanf("%s%lf%s", student, &score, id);
        if(student[0] == 'B') score /= 1.5;
        else if(student[0] == 'T') score *= 1.5;
        string sid = string(id);
        for (int j = 0; j < sid.size(); ++j)
        {
            if(sid[j] >= 'A' && sid[j] <= 'Z') sid[j] = sid[j] - 'A' + 'a';
        }
        mp[sid].id = sid;
        mp[sid].tws += score;
        mp[sid].ns++;
    }
    vector<institution> v;
    for(map<string, institution>::iterator it = mp.begin(); it != mp.end(); it++)
    {
        it->second.tws = (int)it->second.tws;
        v.push_back(it->second);
    }
    sort(v.begin(), v.end());
    int rank = 1, lastRank = 1;
    printf("%d\n", v.size());
    for (int k = 0; k < v.size(); ++k)
    {
        if(k == 0) printf("%d %s %d %d\n", rank, v[k].id.c_str(), (int)v[k].tws, v[k].ns);
        else
        {
            if(v[k].tws == v[k-1].tws) printf("%d %s %d %d\n", lastRank, v[k].id.c_str(), (int)v[k].tws,v[k].ns);
            else
            {
                printf("%d %s %d %d\n", rank, v[k].id.c_str(), (int)v[k].tws, v[k].ns);
                lastRank = rank;
            }
        }
        rank++;
    }
    return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值