文章

27

粉丝

86

获赞

10

访问

21.0k

头像
成绩排序 题解:
P1151 清华大学上机题
发布于2023年8月14日 21:43
阅读数 837

C++

stable_sort()

while(cin>>xxx)

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
struct Student{
	string name;
	int score;
};
bool cmpDesc(const Student& student1, const Student& student2){
	return student1.score > student2.score;
}
bool cmpAsc(const Student& student1, const Student& student2){
	return student1.score < student2.score;
}
int main(){
	int num, descAsc;
	while(cin >> num >> descAsc){
		vector<Student> students;
		for(int i = 0; i < num; i++){
			Student student;
			cin >> student.name >> student.score;
			students.push_back(student);
		}
		if(descAsc == 0){
			stable_sort(students.begin(), students.end(), cmpDesc);
		}
		else if(descAsc == 1){
			stable_sort(students.begin(), students.end(), cmpAsc);
		}
		for(int i = 0; i < students.size(); i++){
			cout << students[i].name << "...
登录查看完整内容


登录后发布评论

暂无评论,来抢沙发