PAT甲级1028 List Sorting (25分)

#pragma warning(disable:4996)
#include
#include
#include
#include
using namespace std;

struct information
{
     
	char id[10];
	char name[10];
	int grade;
}info[100010];

int C;

bool cmp(struct information a, struct information b)
{
     
	if (C == 1)
	{
     
		int s = strcmp(a.id, b.id);
		return s < 0;
	}
	else if (C == 2)
	{
     
		int s = strcmp(a.name, b.name);
		if (s != 0)
		{
     
			return s < 0;
		}
		else
		{
     
			int ss = strcmp(a.id, b.id);
			return ss < 0;
		}
	}
	else if (C == 3)
	{
     
		if (a.grade != b.grade)
			return a.grade < b.grade;
		else
		{
     
			int ss = strcmp(a.id, b.id);
			return ss < 0;
		}
	}
}

int main()
{
     
	int N;
	scanf("%d%d", &N, &C);
	for (int i = 0; i < N; i++)
	{
     
		scanf("%s%s%d", info[i].id, info[i].name, &info[i].grade);
	}
	sort(info, info + N, cmp);
	for (int i = 0; i < N; i++)
	{
     
		printf("%s %s %d\n", info[i].id, info[i].name, info[i].grade);
	}
	return 0;
}

你可能感兴趣的:(PAT)