AcWing 3375. 成绩排序
原题链接
简单
作者:
王杜杜
,
2025-01-13 22:21:44
,
所有人可见
,
阅读 2
C++ 代码
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::stable_sort; // 注意,c++中的std::sort是不稳定排序。此处应是稳定排序
using std::vector;
using std::string;
typedef struct{
string name;
int grade;
}Grade;
bool cmp0(Grade g1, Grade g2){
return g1.grade > g2.grade;
}
bool cmp1(Grade g1, Grade g2){
return g1.grade < g2.grade;
}
int main()
{
int N;
cin >> N;
int flag;
cin >> flag;
vector<Grade> vec;
for(int i=0; i<N; i++){
Grade g;
cin >> g.name >> g.grade;
vec.push_back(g);
}
if(flag == 1)
stable_sort(vec.begin(), vec.end(), cmp1);
else
stable_sort(vec.begin(), vec.end(), cmp0);
for(auto& elem : vec){
cout << elem.name << ' ' << elem.grade << endl;
}
return 0;
}