题目链接:帮贡排序
✔这道题复习了两个点:
1.结构体的命名和使用。(在上一次的训练中使用了结构体,这道题也很快地想起来了,但是一开始的使用不够规范,感觉还需要复习)
附上:结构体用法详解
2.sort函数的使用(这个我忘得干干净净,看了大佬的题解才有点记忆,但用法也并不熟悉)
附上:sort对结构体排序
记得头文件#include<algorithm
✔另外总结这道题解决时候遇到的问题:
读题存在问题,一开始没有理解到需要做两次排序,这导致我连样本点都理解不了,这个问题出现过很多次了,下次一定要好好读题,不要看到很多字就松懈。
#include<iostream>
#include<algorithm>
using namespace std;
typedef struct person{
string name;
string pos;
int bg;
int lev;
int xh;//输入序号
}per;
per team[115];
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;
}
int cmp(per x,per y){
if(x.bg==y.bg) return x.xh<y.xh;
return x.bg>y.bg;
}
int cmp2(per x,per y){
if(x.pos==y.pos){
if(x.lev==y.lev) return x.xh<y.xh;
return x.lev>y.lev;
}
return change(x.pos)<change(y.pos);
}
int main(void)
{
int n;
cin>>n;
for(int i=0;i<n;i++){
cin>>team[i].name>>team[i].pos>>team[i].bg>>team[i].lev;
team[i].xh=i;
}
sort(team+3,team+n,cmp);
//职位调整
for(int i=3;i<n;i++){
if(i<=4) team[i].pos="HuFa";
else if(i>=5&&i<=8) team[i].pos="ZhangLao";
else if(i>=9&&i<=15) team[i].pos="TangZhu";
else if(i>=16&&i<=40) team[i].pos="JingYing";
else team[i].pos="BangZhong";
}
sort(team,team+n,cmp2);
for(int i=0;i<n;i++) cout<<team[i].name<<" "<<team[i].pos<<" "<<team[i].lev<<endl;
return 0;
}