Question 223. 2022 Winter Holiday Ladder Competition Training-7-12 Inventory Code Base (25 points)


Question 223. 2022 Winter Holiday Ladder Competition Training-7-12 Inventory Code Base (25 points)


1. The topic

insert image description here
insert image description here

2. Problem solving

In fact, when I wrote this method, I was easily confused (covering my face. The basic idea is to count the number of each module, and then sort it. The code is as follows:

#include <bits/stdc++.h>

using namespace std;

typedef map<vector<int>,int>::iterator Pos;

map<vector<int>,int> m;//用map存储输出结果序列(由于是map,存放顺序正好可为题目说的递增序)以及出现的次数
map<int,vector<Pos>,greater<int>> res;//用map直接对出现的次数进行了排序,以输出结果序列出现的次数为first(降序),m的每一个元素(就是一个个迭代器)为second(这些迭代器取second就是输出结果序列出现的次数,取first为输出序列)。可以看作是对输出序列出现次数做了一次桶排。

int main()
{
    
    
    int N,M;
    cin>>N>>M;
    for(int i=0;i<N;i++)
    {
    
    
        vector<int> v;//vec存放一行输出结果序列
        for(int j=0;j<M;j++)
        {
    
    
            int out;
            scanf("%d",&out);
            v.push_back(out);
        }
        m[v]++;//对应输出结果序列的个数加1
    }
    for(auto it=m.begin();it!=m.end();it++)
    {
    
    
        res[it->second].push_back(it);//将m的迭代器放入到对应的桶里
    }
    cout<<m.size()<<endl;
    for(auto it=res.begin();it!=res.end();it++)//将res从头遍历到尾结果输出即可
    {
    
    
        for(int i=0;i<it->second.size();i++)
        {
    
    
            printf("%d",it->first);
            for(int j=0;j<it->second[i]->first.size();j++)//切记那是一个桶,可能有好几个输出序列出现的次数是等于当前那个res的first的。
            {
    
    
                putchar(' ');
                printf("%d",it->second[i]->first[j]);
            }
            putchar('\n');
        }
    }
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324345790&siteId=291194637