其他分享
首页 > 其他分享> > PAT A1141 PAT Ranking of Institutions (25 分) 排序

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;
}


 

标签:Ranking,include,PAT,25,int,else,tws,ns,id
来源: https://blog.csdn.net/chch1996/article/details/100578638