1141 PAT Ranking of Institutions

题意:给出考生id(分为乙级、甲级和顶级),取得的分数和所属学校。计算各个学校的所有考生的带权总成绩,以及各个学校的考生人数。最后对学校进行排名。

思路:本题的研究对象是学校,而不是考生!因此,建立学校的结构体Record,记录学校的校名,考生人数,排名,成绩等信息。利用map<校名,该学校对应的结构体>构造映射,然后在读入数据的时候就可以更新该学校的分数,学生人数。数据读入完毕后,根据计算规则求出各个学校的带权总成绩,再把结构体放到vector中排序一下,确定排名,就好了。

代码:

#include <cstdio>
#include <cctype>
#include <string>
#include <vector>
#include <map>
#include <iostream>
#include <algorithm>
#include <fstream>
using namespace std;

struct Record{
    int rank;
    string sch;
    int ScoreB,ScoreA,ScoreT,tws;
    int cnt;
    Record():rank(0),sch(""),ScoreB(0),ScoreA(0),ScoreT(0),tws(0),cnt(0){}
};
map<string,Record> mp;
vector<Record> vec;

bool cmp(Record a,Record b)
{
    if(a.tws!=b.tws) return a.tws>b.tws;
    else if(a.cnt!=b.cnt) return a.cnt<b.cnt;
    else return a.sch<b.sch;
}

int main()
{
    //ifstream cin("pat.txt");
    int n;
    cin>>n;
    string sch,id;
    int score;
    for(int i=0;i<n;i++){
        cin>>id>>score>>sch;
        for(int i=0;i<sch.size();i++)
            sch[i]=tolower(sch[i]);
        mp[sch].cnt++;
        mp[sch].sch=sch;
        if(id[0]=='B') mp[sch].ScoreB+=score;
        else if(id[0]=='A') mp[sch].ScoreA+=score;
        else mp[sch].ScoreT+=score;
    }
    for(auto it:mp){
        Record rec=it.second;
        rec.tws=rec.ScoreB/1.5 + rec.ScoreA + rec.ScoreT*1.5;
        vec.push_back(rec);
    }
    sort(vec.begin(),vec.end(),cmp);
    for(int i=0;i<vec.size();i++){
        if(i==0) vec[i].rank=1;
        else if(vec[i].tws==vec[i-1].tws) vec[i].rank=vec[i-1].rank;
        else vec[i].rank=i+1;
    }
    cout<<vec.size()<<'\n';
    for(auto it:vec)
        cout<<it.rank<<' '<<it.sch<<' '<<it.tws<<' '<<it.cnt<<'\n';
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/kkmjy/p/9568311.html