PTA 1015德才论(C语言实现)

是一种比较呆的想法,算是最笨的一种了

思想就是在读入数据的时候就分类存储,然后对每一类进行分类

#include <iostream>
struct student
{
	long studentID;
	int moral;
	int talent;
};
struct student class_1[100000];
struct student class_2[100000];
struct student class_3[100000];
struct student class_4[100000];
void inputStudent(struct student *p,long x,int y,int z);
void printStudent(struct student p[],long index);
void sortStudent(struct student p[],long left,long right);
long sortStudentClass(struct student p[],long left,long right);
void swapStudent(struct student *p1,struct student *p2);
bool islower(struct student *p1,struct student *p2);
int main(void)
{
	long N = 0;
    int L = 0,H = 0;
	long num = 0;
	int mor = 0,tal = 0;
	scanf("%ld %d %d",&N,&L,&H);

	long numberClass1 = 0,numberClass2 = 0, numberClass3 = 0,numberClass4 = 0;

	long index = 0;
	while (index < N)
	{
		if(scanf("%ld %d %d",&num,&mor,&tal) == 3){
		if ((mor >= L) && (tal >= L))
		{
			if ((mor >= H) && (tal >= H))
				inputStudent(&class_1[numberClass1++],num,mor,tal);
			else if ((mor >= H) && (tal < H))
				inputStudent(&class_2[numberClass2++],num,mor,tal);
			else if ((mor < H) && (mor >= tal))
				inputStudent(&class_3[numberClass3++],num,mor,tal);
			else
				inputStudent(&class_4[numberClass4++],num,mor,tal);
		}
		index++;
        }
	}
// 	putchar('\n');
// 	putchar('\n');
    printf("%ld\n",numberClass1 + numberClass2 + numberClass3 + numberClass4);
	if(numberClass1){sortStudent(class_1,0,numberClass1 - 1);
	printStudent(class_1,numberClass1);}
// 	putchar('\n');
	if(numberClass2) {sortStudent(class_2,0,numberClass2 - 1);
	printStudent(class_2,numberClass2);}
// 	putchar('\n');
	if(numberClass3) {sortStudent(class_3,0,numberClass3 - 1);
	printStudent(class_3,numberClass3);}
// 	putchar('\n');
	if(numberClass4) {sortStudent(class_4,0,numberClass4 - 1);
	printStudent(class_4,numberClass4);}
	return 0;
}
void inputStudent(struct student *p,long x,int y,int z)
{
	p->studentID = x;
	p->moral = y;
	p->talent = z;
}
void printStudent(struct student p[],long index)
{
	long n = 0;
	while (n < index)
	{
		printf("%ld %d %d\n",p[n].studentID,p[n].moral,p[n].talent);
		n++;
	}
}
void sortStudent(struct student p[],long left,long right)
{
	if (left >= right) return;
	long solider = sortStudentClass(p,left,right);
	sortStudent(p,left, solider - 1);
	sortStudent(p,solider + 1,right);
}
long sortStudentClass(struct student p[],long left,long right)
{
	long solider = left;
	long i = left;
	long j = right;
	while (i < j)
	{
		while ((i < j) && islower(&p[j],&p[solider]))	j--;
		while ((i < j) && !islower(&p[i],&p[solider])) i++;
		if (i != j) swapStudent(&p[i],&p[j]);
	}
    if (i == j) swapStudent(&p[i],&p[solider]);
	return i;
}
void swapStudent(struct student *p1,struct student *p2)
{
	struct student p;
	p = *p1;
	*p1 = *p2;
	*p2 = p;
}
bool islower(struct student *p1,struct student *p2)
{	
	if ((p1->moral + p1->talent) == (p2->moral + p2->talent))
	{
		if (p1->moral < p2->moral) return true;
		else if ((p1->moral == p2->moral) && (p1->studentID > p2->studentID)) return true;
		else return false;
	}
	else if ((p1->moral + p1->talent) < (p2->moral + p2->talent))
		return true;
	else 
		return false;

}

	

总的来说感觉还是比较好想的,排序的就是把条件变化了一下,总的来说还是快速排序的思想

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值