文章

211

粉丝

1

获赞

1144

访问

39.7k

头像
成绩排序 题解:相同成绩按输入顺序排 stable_sort
P1151 清华大学上机题
发布于2026年3月15日 16:38
阅读数 194

#include<bits/stdc++.h>
using namespace std;

struct node{
	string name;
	int score;
};	
bool cmp(node a,node b){
	return a.score < b.score;	
}	
bool rcmp(node a,node b){
	return a.score > b.score;	
}
int main(){
	int n;
	while(cin >> n){
		vector<node> stu(n);
		int p;
		cin >> p;
		for(int i=0;i<n;i++)
			cin >> stu[i].name >> stu[i].score;
		if(p == 1){
			stable_sort(stu.begin(),stu.end(),cmp);
			for(auto it:stu)
				cout << it.name << " " << it.score << endl;
		}
		else{
			stable_sort(stu.begin(),stu.end(),rcmp);
			for(auto it:stu)
				cout << it.name << " " << it.score << endl;
		}
	}	
	return 0;
}

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发