题解 | 分组统计

#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辅助一下,然后扫描一下就完事了

全部评论

相关推荐

Yki_:以下条件优先录用: 喜欢去缅北当猪仔的
点赞 评论 收藏
分享
coffrar:全都是已读😅沟通一千五百多个了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务