文章

81

粉丝

0

获赞

275

访问

10.6k

头像
成绩排序 题解:stablesort
P1151 清华大学上机题
发布于2025年3月24日 22:31
阅读数 97

#include<bits/stdc++.h>
using namespace std;
struct stu{
	string n;
	int num;
	stu(string a,int b){
		n=a;
		num=b;
	}
};

bool big(stu a,stu b){
	return a.num>b.num;
}
bool small(stu a,stu b){
	return a.num<b.num;
}
int main(){
	int n;
	int order;
	while(cin>>n>>order){
	vector<stu> s;
	for(int i=0;i<n;i++){
		string name;
		int b;
		cin>>name>>b;
		s.push_back({name,b});
	}
	if(order==1){
		stable_sort(s.begin(),s.end(),small);
	}else{
		stable_sort(s.begin(),s.end(),big);
	}
	for(int i=0;i<n;i++){

		cout<<s[i].n<<" "<<s[i].num<<endl;
	}
	}
	return 0;
	
	

}

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发