成绩排序【清华大学】

mac2022-06-30  218

牛客网题目链接

题解

1.有多组输入 2.如果读入的是0则降序,是1则升序 3.当成绩相同时,需要按照录入在前的先排序

#include <iostream> #include <vector> #include <string> #include <cmath> #include <algorithm> #include <queue> using namespace std; const int N = 1e5+7; struct stu{ string name; int score, index; }E[N]; bool cmp1(stu a, stu b){ if(a.score != b.score) return a.score < b.score; else return a.index < b.index; } bool cmp2(stu a, stu b){ if(a.score != b.score) return a.score > b.score; else return a.index < b.index; } int main(){ int n, t, x; while(cin>>n){ cin>>t; for(int i = 1; i <= n; i++){ cin>>E[i].name>>E[i].score; E[i].index = i; } if(t) sort(E+1, E+n+1, cmp1); else sort(E+1, E+n+1, cmp2); for(int i = 1; i <= n; i++) cout<<E[i].name<<" "<<E[i].score<<endl; } return 0; }

最新回复(0)