OJ_成绩排序

题干

OJ_成绩排序_第1张图片

c++实现

#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
using namespace std;

struct student {
	int id;
	int score;
};

bool compare(student a, student b) {
	if (a.score > b.score) {
		return false;
	}
	else if (a.score == b.score && a.id > b.id) {
		return false;
	}
	else {
		return true;
	}
}

int main() {
	int N;
	scanf("%d", &N);
	vector<student> s(N);
	for (int i = 0; i < N; i++)
	{
		scanf("%d%d", &s[i].id, &s[i].score);
	}
	sort(s.begin(), s.end(), compare);
	for (int i = 0; i < N; i++)
	{
		printf("%d %d\n", s[i].id, s[i].score);
	}
	return 0;
}

你可能感兴趣的:(数据结构与算法,算法)