文章

27

粉丝

86

获赞

10

访问

29.8k

头像
成绩排序2.0 题解:
P1159 清华大学上机题
发布于2023年8月14日 22:14
阅读数 1.0k

C++

cmp函数保证先比较分数score,分数相同比较学号id

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
struct Student{
	int id;
	int score;
};
bool cmp(const Student& student1, const Student& student2){
	if(student1.score == student2.score){
		return student1.id < student2.id;
	}
	return student1.score < student2.score;
}
int main(){
	int N;
	cin >> N;
	vector<Student> students;
	for(int i = 0; i < N; i++){
		Student student;
		cin >> student.id >> student.score;
		students.push_back(student);
	}
	sort(students.begin(), students.end(), cmp);
	for(int i = 0; i < students.size(); i++){
		cout << students[i].id << " " << students[i].score << endl;
	}
	return 0;
}

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发