题解 | 分组统计

#include <bits/stdc++.h>
#include <set>
#include <vector>
using namespace std;

int main() {
    int m;
    while (cin >> m) {
        while (m--) {
            int n;
            cin >> n;
            int a[n], b[n];
            set<int>as, bs;
            for (int i = 0; i < n; i++) {
                cin >> a[i];
                as.insert(a[i]);
            }
            for (int i = 0; i < n; i++) {
                cin >> b[i];
                bs.insert(b[i]);
            }
            for (auto y : bs) {
                map<int, int>mp;
                for (int i = 0; i < n; i++) {
                    if (b[i] == y) {
                        mp[a[i]]++;
                    }
                }
                vector<int>tmp;
                for (auto x : as) {
                    tmp.push_back(x);
                }
                cout << y << "={";
                for (int i = 0; i < tmp.size(); i++) {
                    if (i == tmp.size() - 1)cout << tmp[i] << "=" << mp[tmp[i]];
                    else cout << tmp[i] << "=" << mp[tmp[i]] << ",";
                }
                cout << "}" << endl;
            }
        }
    }
}

看着代码复杂,其实很简单,只需要利用set辅助一下,然后扫描一下就完事了

全部评论

相关推荐

09-10 14:27
中南大学 营销
在提需求的安德鲁很无...:阿q,对面只会觉得好笑
点赞 评论 收藏
分享
头像
08-28 09:05
门头沟学院
投递美团等公司10个岗位
点赞 评论 收藏
分享
09-09 16:12
已编辑
成都理工大学 Java
码客明:拿下hr就拿到offer了
点赞 评论 收藏
分享
海尔太过分了,92学历都被卡死了,你到底要什么样的人才????
皮格吉:一样,不过也干脆,没测评没笔试,直接挂不折腾人。
投递海尔等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务