1028 List Sorting

1028 List Sorting_第1张图片
**题目大意:如果C等于1的话,那就按照ID的大小进行升序排序
如果C等于2的话,就按照名字进行不降序排序
如果C等于3的话,那就按照成绩进行不降序排序
当然题目中有个前提:如果有相同大小的名字或者成绩,则按照ID进行升序排序

1028 List Sorting_第2张图片
1028 List Sorting_第3张图片
1028 List Sorting_第4张图片

#include
using namespace std;
const	 int N = 1e5;
struct student
{
	char id[10];
	char name[10];
	int score;
};  
student stu[N];//如果姓名和成绩相同,则按照学号升序排序
bool cmp1(student a, student b)
{
	return strcmp(a.id, b.id) < 0;
}
bool cmp2(student a, student b)//姓名,按照不降序排列
{
	return strcmp(a.name, b.name) != 0 ? strcmp(a.name, b.name) <= 0 : strcmp(a.id, b.id) < 0;
}
bool cmp3(student a, student b)//成绩,不降序排列
{
	return a.score != b.score ? a.score <= b.score : strcmp(a.id, b.id) < 0;
}
int main()
{
	int N, C;
	cin >> N >> C;
	for (int i = 0; i < N; i++)
		cin >> stu[i].id >> stu[i].name >> stu[i].score;
	if (C == 1)
	{
		sort(stu, stu + N, cmp1);
		for (int i = 0; i < N; i++)
			cout << stu[i].id << " " << stu[i].name << " " << stu[i].score << endl;
	}
	if (C == 2)
	{
		sort(stu, stu + N, cmp2);
		for (int i = 0; i < N; i++)
			cout << stu[i].id << " " << stu[i].name << " " << stu[i].score << endl;
	}
	if (C == 3)
	{
		sort(stu, stu + N, cmp3);
		for (int i = 0; i < N; i++)
			cout << stu[i].id << " " << stu[i].name << " " << stu[i].score << endl;
	}
	return 0;
}

你可能感兴趣的:(PAT)