题解 | #成绩排序#
成绩排序
https://www.nowcoder.com/practice/0383714a1bb749499050d2e0610418b1
结构体 + 优先队列求法
#include<bits/stdc++.h> using namespace std; int n,m; struct User{ string username; int number; int grade; bool operator < (User c) const { if(m == 0){ // 降序 if (grade == c.grade){ return number > c.number; }else{ return grade < c.grade; } }else{ if (grade == c.grade){ return number > c.number; }else{ return grade > c.grade; } } } }; int main(){ while(scanf("%d",&n) != EOF){ priority_queue<User> sortuser; scanf("%d",&m); for(int i=0;i<n;i++){ User user; cin>>user.username; scanf("%d",&user.grade); user.number = i; sortuser.push(user); } while(!sortuser.empty()){ cout << sortuser.top().username; printf(" %d\n",sortuser.top().grade); sortuser.pop(); } } }