1153 Decode Registration Card of PAT (25分)

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
题意:测试者的id分为四个部分;
我们给出三种类型:第一种:按照分数从大到小排,当分数相同,按照id从小到大排;
第二种:求出符合类型二的id数量和分数总和;
第三种:求出符合类型三的id,和数量,按照数量从大到小排,数量相同,按照id从小到大排;
这里排序,最好用unorder_map,输出用printf,否则会超时;

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <unordered_map>
using namespace std;
struct testees{
    string id;
    int score;
}t[10001];
int N,M,type;
bool cmp(testees a,testees b){
    if(a.score == b.score) return a.id < b.id;
    else return a.score > b.score;

}
bool cmp_1(pair<string,int> a,pair<string,int>b){
    if(a.second == b.second) return a.first < b.first;
    else return a.second > b.second;
}
void deal(string s){
    unordered_map<string,int> mp;
    bool flag = false;
    if(type == 1){
        sort(t,t+N,cmp);
        for(int i = 0; i < N; i++){
            if(s == t[i].id.substr(0,1)){
                printf("%s %d\n",t[i].id.c_str(),t[i].score);
                flag = true;
            }
        }
    }
    else if(type == 2){
        int sum = 0, count = 0;
        for(int i = 0; i < N; i++){
            if(s == t[i].id.substr(1,3)){
                sum += t[i].score;
                flag = true;
                count++;
            }
        }
        if(flag) printf("%d %d\n",count,sum);
    }
    else if(type == 3){
        for(int i = 0; i < N; i++){
            if(s == t[i].id.substr(4,6)){
                mp[t[i].id.substr(1,3)]++;
                flag = true;
            }
        }
        vector<pair<string,int>>vec_mp(mp.begin(),mp.end());
        sort(vec_mp.begin(),vec_mp.end(),cmp_1);
        for(int i = 0; i <vec_mp.size(); i++){
            printf("%s %d\n",vec_mp[i].first.c_str(),vec_mp[i].second);
        }
    }
    if(!flag) printf("NA\n");
}
int main(){
    string s;
    cin >> N >> M;
    for(int i = 0;i < N; i++){
        cin >> t[i].id >> t[i].score;
    }
    for(int i = 1; i <= M; i++){
       cin >> type >> s;
       printf("Case %d: %d %s\n",i,type,s.c_str());
       deal(s);
    }
}在这里插入代码片
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值