题解 | #成绩排序#

成绩排序

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

#include<vector>
#include<string>
#include<iostream>
#include<tuple>
#include<algorithm>

using namespace std;

bool compare1(tuple<string,int> x, tuple<string, int> y) {

	return get<1>(x) > get<1>(y);
				
}
bool compare2(tuple<string, int> x, tuple<string, int> y) {

	return get<1>(x) < get<1>(y);

}

int main() {
	int n,mode;
	vector < tuple<string, int> > vec;
	while (cin >> n>> mode)
	{
		vec.clear();
		while (n--)
		{
			string a;
			int b;
			cin >> a >> b;
			vec.emplace_back(tuple<string, int>(a, b));
		}
		if (mode == 0)
			stable_sort(vec.begin(), vec.end(), compare1);
		else
		{
			stable_sort(vec.begin(), vec.end(), compare2);
		}
		for (auto it = vec.begin(); it != vec.end(); it++) {
			cout << get<0>(*it) << " " << get<1>(*it) << endl;
		}
	}
	

}

这题的关键在于:

  1. 使用tuple
  2. 使sort变成稳定的->stable_sort,或者增加一个计数参数
全部评论

相关推荐

小浪_Coding:找硬件测试,也可兼顾软测欧, 简历还可以的 ,注意排版,项目写的有条理一点, 然后个人技能多加点, 润色好简历之后就开始沟通海投了,深圳,东莞这边做硬件相关的公司还不少, 医疗类,仪器类的都可以尝试
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务