题解:sort应用之成绩排序(结构体)
#include<iostream> #include<cstdio> #include<vector> #include<algorithm> using namespace std; struct student{ int number; int score; }; bool compare(student lhs,student rhs){ if(lhs.score<rhs.score) return 1; else if(lhs.score==rhs.score&&lhs.number<rhs.number) return 1; else return 0; } int main(){ int n; scanf("%d",&n); vector<student> vec(n); for(int i=0;i<n;i++){ scanf("%d%d",&vec[i].number,&vec[i].score); } sort(vec.begin(),vec.end(),compare); for (int i=0;i<n;i++){ printf("%d %d\n",vec[i].number,vec[i].score); } return 0; }
ACWING 3376