题解 | 成绩排序
成绩排序
https://www.nowcoder.com/practice/3f27a0a5a59643a8abf0140b9a8cf1f7
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
struct stu{
int id;
int score;
};
bool operator<(stu a, stu b){
if(a.score != b.score) return a.score>b.score;
else return b.id<a.id;
}
priority_queue<stu> s;
void spush(int id, int score){
s.push({id,score});
}
void spop(){
if(!s.empty()){
s.pop();
}
}
stu stop(){
if(!s.empty()){
return s.top();
}return {};
}
int main() {
int n;
cin >> n;
int count = n;
while(count--){
int id, score;
cin >> id >> score;
spush(id, score);
}
while(n--){
stu s = stop();
cout << s.id << " " << s.score << endl;
spop();
}
}
// 64 位输出请用 printf("%lld")
结构体优先队列进行求解:需要定义operator函数,使其按照成绩/学号进行排序;定义pop()、push()、top()函数并输出
查看11道真题和解析