结构体数组排序复杂版

版权声明:小简原创 https://blog.csdn.net/qq_43469554/article/details/87639930
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

struct Student{
	string name;
	int score[4];
};

int cmp(Student x, Student y)
{
	if (x.score[0] != y.score[0])
	{
		return x.score[0] > y.score[0];
	}
	if (x.score[1] != y.score[1])
	{
		return x.score[1] > y.score[1];
	}
	if (x.score[2] != y.score[2])
	{
		return x.score[2] > y.score[2];
	}
	return x.score[3] > y.score[3];
}

int main()
{
	Student stu[3];
	for (int i = 0; i < 3; i++)
	{
		cin >> stu[i].name;
		for (int j = 0; j < 4; j++)
		{
			cin >> stu[i].score[j];
		}
	}
	sort(stu, stu + 3, cmp);
	for (int i = 0; i < 3; i++)
	{
		cout << stu[i].name << ":";
		for (int j = 0; j < 4; j++)
		{
			cout << stu[i].score[j] << " ";
		}
		cout << endl;
	}
	return 0;
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_43469554/article/details/87639930