AcWing
  • 首页
  • 活动
  • 题库
  • 竞赛
  • 应用
  • 更多
    • 题解
    • 分享
    • 商店
    • 问答
    • 吐槽
  • App
  • 登录/注册

AcWing 3576. 分组统计    原题链接    简单

作者: 作者的头像   刘华强很强 ,  2023-05-26 16:33:33 ,  所有人可见 ,  阅读 33


0


分组统计

思路

一周了,重新写这个代码一遍过。
基本就是用数组进行存储,map进行统计。
之后对于数组进行去重,方便后续输出
然后遍历hash,找到每一组下的统计情况,然后进行输出就可以

代码

#include<iostream>
#include<vector>
#include<map>
#include<algorithm>
#include<cstring>
using namespace std;
int t;
const int  N = 110;
int n;
int a[N];
int cnt[1010];
int main()
{
    cin>>t;
    while(t--){
        cin>>n;
        vector<int> v;
        for(int i=0;i<n;i++){
            cin>>a[i];
            v.push_back(a[i]);
        }
        map<int,vector<int>> hash;
        for(int i=0;i<n;i++)
        {
            int x;
            cin>>x;
            hash[x].push_back(a[i]);
        }
        //处理后续的操作,erase是vector中的函数
        sort(v.begin(),v.end());
        v.erase(unique(v.begin(),v.end()),v.end());

        int timer = 1;
        for(auto x:hash){
            memset(cnt,0,sizeof cnt);
            for(auto y:x.second) cnt[y]++;
            cout<<timer<<"={";
            for(int i = 0;i<v.size();i++)
            {
                if(i!=v.size()-1) cout<<v[i]<<"="<<cnt[v[i]]<<",";
                else cout<<v[i]<<"="<<cnt[v[i]];
            }
            cout<<"}"<<endl;
            timer++;
        }

    }
}

0 评论

你确定删除吗?

© 2018-2023 AcWing 版权所有  |  京ICP备17053197号-1
用户协议  |  隐私政策  |  常见问题  |  联系我们
AcWing
请输入登录信息
更多登录方式: 微信图标 qq图标 qq图标
请输入绑定的邮箱地址
请输入注册信息