PAT-A 1141题解

代码

算法的具体过程体现在代码中了。由于本人喜欢写比较长但是清晰易懂的代码,所以各位看官看个乐乎就行

#include<vector>
#include<iostream>
#include<string>
#include<algorithm>
#include<unordered_map>
#include<set>
#include<cstring>
using namespace std;
//结构体,用于存放最后的学校信息
struct Institution
{
    string name;
    int cnt;
    int score;
};
bool cmp(const Institution &a,const Institution &b){
    if(a.score != b.score){
        return a.score > b.score;
    }
    else if(a.cnt != b.cnt){
        return a.cnt<b.cnt;
    }
    else{
        return a.name<b.name;
    }
}
//由于不需要学生的具体信息,这个map对象就用于学校和它们成绩之间的映射关系
unordered_map<string,pair<int,double> > mp;
//由于有很多重复的学校,这个schools集合用于确定最后学校的数量
set<string> schools;
//大小写转换函数
void toLowerCase(string &str){
    for(int i = 0;i<str.length();i++){
        if(str[i] >= 'A' && str[i] <= 'Z'){
            str[i] = str[i]-'A'+'a';
        }
    }
}
int main(){
    int n;
    cin>>n;
    for(int i = 0;i<n;i++){
        string str1,str2;
        double num;
        cin>>str1>>num>>str2;
        toLowerCase(str2);
        schools.insert(str2);
        mp[str2].first++;
        double tmp_socre;
        if(str1[0] == 'B'){
            tmp_socre = num/1.5;
        }
        else if(str1[0] == 'T'){
            tmp_socre = num*1.5;
        }
        else{
            tmp_socre = num;
        }
        mp[str2].second += tmp_socre;
    }
    vector<Institution> ans;
    //从set中遍历所有的元素,然后把有这些学校的信息放到vector中进行维护
    for(string s : schools){
        ans.push_back(Institution{s,mp[s].first,mp[s].second});
    }
    sort(ans.begin(),ans.end(),cmp);
    printf("%d\n",schools.size());
    int preIndex = 1,preScore = -1,index;
    for(int i = 0;i<ans.size();i++){
        
        if(ans[i].score == preScore || preScore == -1)
        {
            index = preIndex;
        }
        else{
            index = i + 1;
        }
        preIndex = index;
        preScore = ans[i].score;
        printf("%d %s %d %d\n",index,ans[i].name.c_str(),ans[i].score,ans[i].cnt);
    }
    system("pause");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值