1062 Talent and Virtue

纯排序

给结构体的char[]数组赋值时用strcpy

#include <stdio.h>
#include <string.h>
#include <algorithm>

using namespace std;

int n, L, H;


typedef struct person{
	char id[10];
	int vir;
	int tal;
	int sum;
	int type;//0: sage, 1: nobleman, 2: foolman, 3: samll man
};

person ps[100000+5];

int cmp(const void * a, const void * b){
	person pa = *(person *)a;
	person pb = *(person *)b;

	if(pa.type != pb.type){
		return pa.type -pb.type;
	}else if(pa.sum != pb.sum){
		return pb.sum-pa.sum;
	}else if(pa.vir != pb.vir){
		return pb.vir-pa.vir;
	}

	return strcmp(pa.id, pb.id);

}


int main(){
	freopen("in.txt","r",stdin);

	scanf("%d%d%d",&n,&L, &H);

	int m = 0;

	for(int i = 0; i < n; i++){
		char id[10];
		int vir, tal;
		scanf("%s%d%d", &id, &vir, &tal);
		if(vir >= L && tal >= L){
			person p;
			strcpy(p.id,id);//复制字符数组用strcpy
			p.vir = vir;
			p.tal = tal;

			if(vir >= H && tal >= H){
				p.type = 0;
			}else if(vir >= H && tal<H){
				p.type = 1;
			}else if(vir < H && vir >= tal){
				p.type = 2;
			}else{
				p.type = 3;
			}
			p.sum = vir+tal;

			ps[m++] = p;
		}
	}

	printf("%d\n",m);

	qsort(ps,m,sizeof(person),cmp);

	for(int i = 0; i < m; i++){
		printf("%s %d %d\n",ps[i].id, ps[i].vir, ps[i].tal);
	}


	return 0;
}


你可能感兴趣的:(1062 Talent and Virtue)