题解 | #成绩排序#

成绩排序

https://www.nowcoder.com/practice/8e400fd9905747e4acc2aeed7240978b

// HJ68-2成绩排序.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
#include<iostream>
#include<bits/stdc++.h>

using namespace std;

int main()
{
	int n,k;
	while (cin >> n>>k)
	{
		vector<pair<string, int>>mm(n);
		for (int i = 0; i < n; i++)
		{
			string tmp = "";
			int x = 0;
			cin >> tmp >> x;
			mm[i].first = tmp;
			mm[i].second = x;
		}if (k == 0)
		{
			stable_sort(mm.begin(), mm.end(), [](const pair<string, int>& a, const pair<string, int>& b)
				{
					return a.second > b.second;
				});
		}
		else
		{
			stable_sort(mm.begin(), mm.end(), [](const pair<string, int>& a, const pair<string, int>& b)
				{
					return a.second < b.second;
				});
		}
		for (auto c : mm)
		{
			cout << c.first <<" "<< c.second << endl;
		}
	}
	
	return 0;
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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