题解 | #成绩排序#
自定义比较器
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
typedef struct{
int ID;
int goal;
}student;
bool cmp(const student s1,const student s2){
// 牢记true是返回前面的
if(s1.goal<s2.goal) return true;
else if(s1.goal > s2.goal) return false;
else if(s1.ID < s2.ID) return true;
else return false;
}
int main(){
int n;
cin>>n;
vector<student> st;
int p,q = 0;
while(cin>>p&&cin>>q){
student s;
s.ID = p;
s.goal = q;
st.push_back(s);
}
sort(st.begin(),st.begin()+st.size(),cmp);
for(student item : st){
cout<<item.ID<<" "<<item.goal<<" \n";
}
}