AcWing 3375. 成绩排序
原题链接
简单
作者:
小黄鸭斗地主
,
2023-01-03 17:47:16
,
所有人可见
,
阅读 164
#include<iostream>
#include<algorithm>
int n,m;
struct Student {
int score;
char name[100];
bool operator < (const Student &t) const {
return score < t.score;
}
bool operator > (const Student &t) const {
return score > t.score;
}
}student[1010];
using namespace std;
int main() {
cin >> n;
cin >> m;
//从低到高
for (int i = 0; i < n; i++) {
cin >> student[i].name >> student[i].score;
}
if (m == 1) {
stable_sort(student, student+n);
} else { //从高到低
stable_sort(student, student+n,greater<Student>());
}
for (int i = 0; i < n; i++) {
cout << student[i].name << " " << student[i].score << endl;
}
return 0;
}