题解 | #成绩排序#
stable_sort 稳定排序
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
typedef struct{
string name;
int goal;
int num;
}student;
bool cmp1(student s1,student s2){
// if(s1.goal<s2.goal) return true;
// else if(s1.goal==s2.goal) return true;
// else return false;
return s1.goal<s2.goal;
}
bool cmp2(student s1,student s2){
// if(s1.goal>s2.goal) return true;
// else if(s1.goal==s2.goal) return true;
// else return false;
return s1.goal>s2.goal;
}
int main(){
int n;
int tag;
while(cin>>n&&cin>>tag){
vector<student> st;
string name; int goal;
for(int i=0;i<n;i++){
cin>>name>>goal;
student s;
s.name = name;
s.goal = goal;
st.push_back(s);
}
if(tag == 1){
stable_sort(st.begin(),st.begin()+st.size(),cmp1);
}else if(tag == 0){
stable_sort(st.begin(),st.begin()+st.size(),cmp2);
}
for(student item: st){
cout<<item.name<<" "<<item.goal<<"\n";
}
}
}