PAT A1153 Decode Registration Card of PAT (25 分) 排序 map

    题目大意:给出N名考生的准考证号和成绩,要求按三种方式进行查询:① 根据等级查询,查询所有等级为 T/A/B的考生。② 根据考场查询,输出查询考场的总人数和总成绩。③ 根据考试日期查询,输出在查询日期那一天 有考生参加考试的考场的序号,以及该考场在那一天的总人数和总成绩。

    还是阅读理解题。查询①容易做到,将对应等级的考生存放在 map<string, vector<student>>内,排序后输出vector<student>即可。查询②也容易做到,由于考场号在101~999,可以建立大小为1000的数组,用来记录每个考场的考生人数和总成绩。查询③麻烦一些,它的要求是输出在指定日期的有考生参考的考场信息。因此,这个map的类型应当是 map<string, map<int, site>>,第一个string是日期,map<int, site>中的int是考场号,而site结构体存放的是该考场的信息,包括考场号、考生人数和总成绩。因为最后还要对site进行排序,所以应当使用结构体存储。最后,遍历map<int,site>,存放到vector<site>中进行排序。

AC代码:

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

struct student
{
    string card;
    int score;
    bool operator<(const student& other)
    {
        if(this->score != other.score) return this->score > other.score;
        else return this->card < other.card;
    }
};

struct site
{
    int index;
    int nt;
    int ns;
    bool operator<(const site& other)
    {
        if(this->nt != other.nt) return this->nt > other.nt;
        else return this->index < other.index;
    }
};

map<string, vector<student>> levelList;
vector<site> siteList(1010);
map<string, map<int, site>> dateList;

int main()
{
    int N, M;
    cin >> N >> M;
    for (int i = 0; i < N; ++i)
    {
        string card;
        int score;
        cin >> card >> score;
        student now = {card,score};
        if(card[0] == 'T') levelList["T"].push_back(now);
        else if(card[0] == 'A') levelList["A"].push_back(now);
        else if(card[0] == 'B') levelList["B"].push_back(now);
        int site = stoi(card.substr(1, 3));
        siteList[site].nt ++;
        siteList[site].ns += score;
        string date = card.substr(4, 6);
        dateList[date][site].index = site;
        dateList[date][site].nt++;
        dateList[date][site].ns += score;
    }
    for (int q = 1; q <= M; ++q)
    {
        int queryId;
        string query;
        cin >> queryId >> query;
        printf("Case %d: %d %s\n", q, queryId, query.c_str());
        if(queryId == 1)
        {
            if(levelList.find(query) != levelList.end())
            {
                sort(levelList[query].begin(), levelList[query].end());
                for (int i = 0; i < levelList[query].size(); ++i)
                {
                    printf("%s %d\n", levelList[query][i].card.c_str(), levelList[query][i].score);
                }
            }
            else printf("NA\n");
        }
        else if(queryId == 2)
        {
            int site = stoi(query);
            if(site >= 101 && site <= 999 && siteList[site].nt > 0) printf("%d %d\n", siteList[site].nt, siteList[site].ns);
            else printf("NA\n");
        }
        else if(queryId == 3)
        {
            if(dateList.find(query) != dateList.end())
            {
                vector<site> v;
                for(auto it = dateList[query].begin(); it != dateList[query].end(); it++)
                {
                    v.push_back(it->second);
                }
                sort(v.begin(), v.end());
                for (int i = 0; i < v.size(); ++i)
                {
                    printf("%d %d\n", v[i].index, v[i].nt);
                }
            }
            else printf("NA\n");
        }
    }
    return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值