题解 | #成绩排序#

成绩排序

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,或者增加一个计数参数
全部评论

相关推荐

点赞 评论 收藏
分享
zYvv:双一流加大加粗再标红,然后广投。主要是获奖荣誉不够,建议开始不用追求大厂,去别的厂子刷下实习。
点赞 评论 收藏
分享
Yki_:你要算时间成本呀,研究生两三年,博士三四年,加起来就五六年了,如果你本科去腾讯干五年,多领五年的年薪,加上公司内涨薪,可能到时候十五年总薪资也跟博士差不多
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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