文章
4
粉丝
140
获赞
8
访问
39.4k
#include<bits/stdc++.h>
using namespace std;
int main(){
	multimap<int, string> mmap;//构造一个允许键的值重复的map储存(分数,姓名)
	string name = "";
	int score = 0; 
	int num = 0, type = 0;;
	cin >> num >> type;//输入要排序人的个数及排序方式:0降序 1升序 
	
	int i = 0;
	while(i < num && (cin >> name >> score)){
		mmap.emplace(score, name);//默认升序排列且稳定
		i++;	
	}
	
	if(type == 0){//如果是降序排列,倒着输出即可 
		for(auto j = mmap.rbegin(); j != mmap.rend(); j++)//这里j是反向迭代器 
			cout << j->second << ' ' << j->first << endl;
	}else{//升序正着输出 
		for(auto j = mmap.begin(); j != mmap.end(); j++)
			cout << j->second << ' ' << j->first << endl;
	}
	
	return 0;
}
登录后发布评论
暂无评论,来抢沙发