-------------------不敢保证全对,,没有那么多测试案例,,如果有错的话 估计就是回调函数部分可能需要修改,,其他部分应该没有问题, ----------------------来自OUC的某同学
#include <iostream>
#include<vector>
#include<iterator>
#include<algorithm>
#include<functional> //仿函数所需头文件
#include<map>
#include <unordered_map>
using namespace std;
//将薪水 薪水出现的次数 薪水出现的顺序 进行封装
class A
{
public:
A():salary(0),count(0),order(0){}
A(int a,int b,int c):salary(a),count(b),order(c){}
int salary;
int count;
int order;
};
//回调函数 此处写的有点啰嗦 为了便于阅读用
bool cmp(A a,A b)
{
if(a.count>b.count) //先按次数排序 再按照顺序
return true;
else if(a.count==b.count) //次数相同的情况下 再按照出现顺序排序
return a.order<b.order;
else
return false;
}
//自己写的接口
vector<int> salarySort1(vector<int>&inputs)
{
vector<int> result; //存储最终排好的序列
unordered_map<int,int> frequency; //用来记录每种工资出现的次数
unordered_map<int,int> order; //用来记录每种工资出现的顺序
int count=1; //记录每种薪资出现的顺序
for(auto&i:inputs){
//统计工资出现的次数
if(frequency.find(i)==frequency.end())
frequency[i]=1;
else
frequency[i]+=1;
//统计工资出现顺序
if(order.find(i)==order.end()) //之前没有出现过
order[i]=count++;
}
vector<A>tmp;
for(auto it=order.begin();it!=order.end();it++)
tmp.push_back(A((*it).first,frequency[(*it).first],(*it).second));
sort(tmp.begin(),tmp.end(),cmp);
for(auto& i:tmp)
{
for(int j=0;j<i.count;j++)
result.push_back(i.salary);
}
return result;
}
//这个是中兴提供的接口,只需要把自己的接口封装在里面 即可实现
vector<int> salarySort(int num,int*p) //注意变量名可能不一样
{
vector<int>input(p,p+num);
return salarySort1(input);
}
int main()
{
//测试案例通过
int a[12]={1,1,2,2,2,3,4,3,4,7,7,7};
vector<int>result= salarySort1(12,a);
for(auto&i:result)
cout<<i<<" ";
}