//使用stable_sort()//稳定排序
//稳定排序,如是两个判断的数值一样,相对顺序不变,满足题意,但是不能ac,为何,必须改成和题解1一样的:在结构体中加入该元素输入时间的成员,自定义比较函数cmp,为什么,stable_sort不是稳定排序吗?
//下面是使用stable_sort函数,更改后cmp函数,ac的代码,没更改cmp函数的为第二段代码
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
struct people {
string name;
string position;
int devote;
int rank;
int h;
};
bool cmp(people p1, people p2)
{
if (p1.devote == p2.devote)
return p1.h < p2.h;
else
return p1.devote > p2.devote;
}
int change(string a) {
if (a == "BangZhu") return 0;
if (a == "FuBangZhu") return 1;
if (a == "HuFa") return 2;
if (a == "ZhangLao") return 3;
if (a == "TangZhu") return 4;
if (a == "JingYing") return 5;
if (a == "BangZhong") return 6;
}
bool cmp1(people p1, people p2)
{
if (p1.position == p2.position) {
if (p1.rank == p2.rank)
return p1.h < p2.h;
else
return p1.rank > p2.rank;
}
else
return change(p1.position) < change(p2.position);
}
int main()
{
int n;
cin >> n;
people p[111];
int col = 3;
for (int i = 1; i <= n; i++) {
cin >> p[i].name >> p[i].position >> p[i].devote >> p[i].rank;
p[i].h = i;
}
stable_sort(p + 4, p + n + 1, cmp);
for (int i = 4; i <= n; i++) {
if (i == 4 || i == 5)
p[i].position = "HuFa";
else if (6 <= i && i <= 9)
p[i].position = "ZhangLao";
else if (10 <= i && i <= 16)
p[i].position = "TangZhu";
else if (17 <= i && i <= 41)
p[i].position = "JingYing";
else
p[i].position = "BangZhong";
}
stable_sort(p + 1, p + n + 1, cmp1);
for (int i = 1; i <= n; i++)
cout << p[i].name << " " << p[i].position << " " << p[i].rank << endl;
return 0;
}
//没更改cmp函数:
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
struct people {
string name;
string position;
int devote;
int rank;
};
bool cmp(people p1, people p2)
{
return p1.devote > p2.devote;
}
int change(string a) {
if (a == "BangZhu") return 0;
if (a == "FuBangZhu") return 1;
if (a == "HuFa") return 2;
if (a == "ZhangLao") return 3;
if (a == "TangZhu") return 4;
if (a == "JingYing") return 5;
if (a == "BangZhong") return 6;
}
bool cmp1(people p1, people p2)
{
if (p1.position == p2.position) {
return p1.rank > p2.rank;
}
else
return change(p1.position) < change(p2.position);
}
int main()
{
int n;
cin >> n;
people p[111];
int col = 3;
for (int i = 1; i <= n; i++) {
cin >> p[i].name >> p[i].position >> p[i].devote >> p[i].rank;
}
stable_sort(p + 4, p + n + 1, cmp);
for (int i = 4; i <= n; i++) {
if (i == 4 || i == 5)
p[i].position = "HuFa";
else if (6 <= i && i <= 9)
p[i].position = "ZhangLao";
else if (10 <= i && i <= 16)
p[i].position = "TangZhu";
else if (17 <= i && i <= 41)
p[i].position = "JingYing";
else
p[i].position = "BangZhong";
}
stable_sort(p + 1, p + n + 1, cmp1);
for (int i = 1; i <= n; i++)
cout << p[i].name << " " << p[i].position << " " << p[i].rank << endl;
return 0;
}
求大佬指点迷津!谢谢!