PAT (Advanced Level) 1028 List Sorting (字符串处理语法题)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/w419387229/article/details/81739438

 字符串的语法不是很熟悉,都记一下

#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

int n,c;
struct People{
	char id[7],name[9];
	int grade;
}people[100005];
bool cmp1(const People &a, const People &b){
	int res = strcmp(a.id, b.id);
	return res < 0;
}
bool cmp2(const People &a, const People &b){
	int res = strcmp(a.name, b.name);
	if(res != 0)
		return res < 0;
	else{
		res = strcmp(a.id, b.id);
		return res < 0;
	}
}
bool cmp3(const People &a, const People &b){
	if(a.grade != b.grade)
		return a.grade < b.grade;
	else{
		int res = strcmp(a.id, b.id);
		return res < 0;
	}
}
int main(){
	scanf("%d%d",&n,&c);
	for(int i = 0; i < n; ++i){
		scanf("%s %s %d",people[i].id,people[i].name,&people[i].grade);
	}
	if(c == 1)
		sort(people,people+n,cmp1);
	else if(c == 2)
		sort(people,people+n,cmp2);
	else
		sort(people,people+n,cmp3);
	for(int i = 0; i < n; ++i){
		printf("%s %s %d\n",people[i].id,people[i].name,people[i].grade);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/w419387229/article/details/81739438