题解 | #成绩排序#
成绩排序
http://www.nowcoder.com/practice/0383714a1bb749499050d2e0610418b1
- 使用vector容器创建不定长结构体数组
- stable_sort为C++封装的稳定排序算法
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
bool flag;
struct stu{
string name;
int score;
};
bool cmp(stu a,stu b) {
return flag ? a.score < b.score:a.score > b.score;
}
int main() {
int n;
while (cin>>n) {
cin >> flag;
vector<stu> student(n);
for (auto& s : student) {
cin >> s.name >> s.score;
}
stable_sort(student.begin(), student.end(), cmp);
for (auto s : student) {
cout << s.name << ' ' << s.score << endl;
}
}
return 0;
}