1028. List Sorting (25)

考察结构体排序

#include<iostream>
#include<algorithm>
#include<vector>
#include<string.h>

typedef struct Student
{
	int id;
	char name[20];
	int g;
}Student;

int C;
bool cmp(Student a, Student b)
{
	if(C == 1)
		return a.id < b.id;
	else if(C == 2)
	{
		if( strcmp(a.name, b.name) == 0 )
			return a.id < b.id;
		else return strcmp(a.name, b.name)<0;
	}
	else if(C == 3)
	{
		if(a.g == b.g)
			return a.id < b.id;
		else return a.g < b.g;
	}
}
int main()
{
	int N;
	while( scanf("%d%d", &N, &C) != EOF )
	{
		std::vector<Student> stuVec(N);
		for(int i = 0; i < N; ++i)
			scanf("%d%s%d", &stuVec[i].id, stuVec[i].name, &stuVec[i].g);
		//sort
		std::sort(stuVec.begin(), stuVec.end(), cmp);
		for(int i = 0; i < N; ++i)
			printf("%06d %s %d\n", stuVec[i].id, stuVec[i].name, stuVec[i].g);
	}
	return 0;
}


 

你可能感兴趣的:(pat,ZJU)