思路比较繁琐,没仔细思考 仅仅实现了功能
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
using namespace std;
bool cmp(pair<int, int> a, pair<int, int> b)
{
return a.second > b.second;
}
int main()
{
vector<int> v {1,1,1,1,3,3,3,4,5,2,6,6,6,6,6};
map<int, int> m;
for(int i = 0; i < v.size(); ++i)
{
if(m.find(v[i]) == m.end())
{
m.insert(make_pair(v[i], 1));
}
else
{
++m[v[i]];
}
}
vector<pair<int, int>> res;
set<int> s;
for(int i = 0; i < v.size(); ++i)
{
if(s.find(v[i]) == s.end())
{
res.push_back(make_pair(v[i], m[v[i]]));
s.insert(v[i]);
}
}
stable_sort(res.begin(), res.end(), cmp);
for(int i = 0; i < res.size(); ++i)
{
for(int j = 0; j < res[i].second; ++j)
{
if(i == res.size() - 1 && j == res[i].second - 1)
{
cout << res[i].first << endl;
}
else
{
cout << res[i].first << " ";
}
}
}
}