文章

59

粉丝

0

获赞

320

访问

7.4k

头像
成绩排序 题解:C++ 运用结构体、容器、stable_sort
P1151 清华大学上机题
发布于2026年3月9日 23:43
阅读数 156

#include <cstdio>
#include <algorithm>
#include <string>
#include <iostream>
#include <vector>
using namespace std;
typedef struct{
	string name;
	int score;
}stu;
bool comp_des(stu x,stu y)
{
	return x.score>y.score;
}
bool comp_asc(const stu &x,const stu &y)
{
	return x.score<y.score;
}
int main()
{
	int n, method;
	string s;
	int num;
	vector<stu> se;//se sequence
	while (cin >> n >> method)
	{
		for (int i = 0; i < n; i++)
		{
			cin >> s >> num;
			se.push_back({ s,num });
		}
		if (method)
			stable_sort(se.begin(), se.end(),comp_asc);
		else
			stable_sort(se.begin(), se.end(), comp_des);
		for (int i = 0; i < n; i++)
			cout << se[i].name << " " << se[i].score << endl;
		se.clear();
	}
	return 0;
}
	
	
	

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发