0
点赞
收藏
分享

微信扫一扫

PTA 1015 德才论

攻城狮Chova 2022-01-16 阅读 82

这个题主要侧重排序算法

实现方法:利用C语言的qosrt函数;先将人群根据题意分为五个type,在排序函数中先排序大类别,然后按照不同类别内部进行排序。

//独立思考,禁copy
#define _CRT_SECURE_NO_DEPRECATE
#pragma warning(disable:4996)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>	
struct member
{
	char num[10];
	int de;
    int cai;
    int sum;
	int type;
}stu[100010];
cmp(struct member* a, struct member* b)
{
	if (a->type != b->type)
		return a->type - b->type;
	else if
		(a->sum != b->sum) return b->sum - a->sum;
	else if
		(a->de != b->de) return b->de - a->de;
	else
		return strcmp(a->num, b->num);
}
int main()
{
	int N, L, H;
	scanf("%d%d%d", &N, &L, &H);
	int current = N;
	for (int i = 0; i < N; i++)
	{
		scanf("%s %d %d", &stu[i].num, &stu[i].de, &stu[i].cai);
		stu[i].sum = stu[i].de + stu[i].cai;
		if (stu[i].de < L || stu[i].cai < L)
		{
			stu[i].type = 5;
			current--;
		}
		else if(stu[i].de >= H && stu[i].cai >= H) 
			stu[i].type = 1;
		else if(stu[i].de >= H && stu[i].cai < H) 
			stu[i].type = 2;
		else if(stu[i].de < H && stu[i].cai < H && stu[i].de >= stu[i].cai)
			stu[i].type = 3;
		else
			stu[i].type = 4;
	}
	qsort(stu, N, sizeof(stu[0]), cmp);
	printf("%d\n", current);
	for (int i = 0; i < current; i++)
	{
		printf("%s %d %d\n", stu[i].num, stu[i].de, stu[i].cai);
	}
	return 0;
}
举报

相关推荐

0 条评论