OJ_成绩排序2

题干

OJ_成绩排序2_第1张图片

c++实现

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

struct student {
	char name[16];
	int score;
	int seq;
};

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

bool comparefromlowtohigh(student a, student b) {
	if (a.score < b.score) {
		return true;
	}
	else if (a.score == b.score && a.seq < b.seq) {
		return true;
	}
	else {
		return false;
	}
}

int main() {
	int N;
	scanf("%d", &N);
	vector<student> s(N);
	int flag;
	scanf("%d", &flag);

	for (int i = 0; i < N; i++)
	{
		scanf("%s%d", &s[i].name, &s[i].score);
		s[i].seq = i;
	}
	if (flag == 0) {
		sort(s.begin(), s.end(), comparefromhightolow);
	}
	else {
		sort(s.begin(), s.end(), comparefromlowtohigh);
	}

	for (int i = 0; i < N; i++)
	{
		printf("%s %d\n", s[i].name, s[i].score);
	}
	return 0;
}

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