1095 解码PAT准考证 大数据处理

文章目录

题解

大数据处理
这样的大数据的题,首先要确定一个方便的数据结构
我来说一下我的数据结构,当然不是最好的,我见我老师的代码比我快挺多
数据结构分析:
1. “类型 为 1 表示要求按分数非升序输出某个指定级别的考生的成绩,对应的 指令 则给出代表指定级别的字母;”
map<char, vector<pair<string, int>>> A;
让级别对应一个集合,集合里存放这个人的id和分数
2. “类型 为 2 表示要求将某指定考场的考生人数和总分统计输出,对应的 指令 则给出指定考场的编号;”
map<string, pair<int, int>> B;
考场对应人数和总分
3. “为 3 表示要求将某指定日期的考生人数分考场统计输出,对应的 指令 则给出指定日期,格式与准考证上日期相同。”

struct node
{
map<string, int> rap;
};
map<string, node> C;

这个稍微难点,我认为
日期对应考场和人数,node里的rap是考场对应人数
因为人数是随着数据变化的,而考场号却不变,必须让不边的对应变化的。但是之后的排序就有点麻烦,得先拿出到vector里,在sort排序
C语言风格的字符串
本来我的这个结构是超时的,我用的是cin,cout
然后换成了scanf和printf就OK了
但是string,并不能直接换啊。我的代码中用的是C语言风格的string
具体更详细的用法可以留言或者私聊

Code

#include <iostream>
#include <map>
#include <set>
#include <algorithm>
#include <vector>
using namespace std;
// 级别
map<char, vector<pair<string, int>>> A;
//               人数   总分
map<string, pair<int, int>> B;
//                     人数  考场
struct node
{
    map<string, int> rap;
};
map<string, node> C;
bool cmp(pair<string, int> e1, pair<string, int> e2)
{
    if (e1.second == e2.second)
        return e1.first < e2.first;
    return e1.second > e2.second;
}
int main()
{
    // cin.sync_with_stdio(0);
    int N, M;
    scanf("%d %d\n", &N, &M);
    string s;
    int x;
    s.resize(13);
    while (N--)
    {
        scanf("%s%d", &s[0], &x);
        char c = s[0];
        string num = s.substr(1, 3);
        string data = s.substr(4, 6);
        A[c].push_back({s, x});
        B[num].first++;
        B[num].second += x;
        ++C[data].rap[num];
    }
    char c;
    for (int i = 1; i <= M; i++)
    {
        scanf("%d", &x);
        printf("Case %d: %d ", i, x);
        if (x == 1)
        {
            scanf(" %c", &c);
            printf("%c\n", c);
            if (A.count(c) == 0)
            {
                cout << "NA\n";
                continue;
            }
            sort(A[c].begin(), A[c].end(), cmp);
            for (auto e : A[c])
                printf("%s %d\n", e.first.c_str(), e.second);
        }
        else if (x == 2)
        {
            cin >> s;
            printf("%s\n", s.c_str());
            if (B.count(s) == 0)
            {
                cout << "NA\n";
                continue;
            }
            printf("%d %d\n", B[s].first, B[s].second);
        }
        else
        {
            cin >> s;
            printf("%s\n", s.c_str());
            if (C.count(s) == 0)
            {
                cout << "NA\n";
                continue;
            }
            vector<pair<string, int>> v;
            for (auto e : C[s].rap)
            {
                v.push_back(e);
            }
            sort(v.begin(), v.end(), cmp);
            for (auto e : v)
                printf("%s %d\n", e.first.c_str(), e.second);
        }
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值