题解 | #成绩排序#

成绩排序

http://www.nowcoder.com/practice/0383714a1bb749499050d2e0610418b1

  1. 使用vector容器创建不定长结构体数组
  2. 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;
}
全部评论

相关推荐

头像 头像
05-06 18:28
已编辑
Java
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务