1015 德才论
这道题自己开始做起来还是挺难的,先分类后冒泡排序,但一般会有测试点超过,果然。后面尝试使用C语言的qsort(),成功了,从这道题,对qsort()使用变得深入很多。
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int id;
int d;
int c;
}Stu;
int low = 0, high = 0;
int check(const Stu *a)
{
if(a->d >= high && a->c >= high)
return 1;
else if(a->d >= high && a->c >= low)
return 2;
else if(a->d >= low && a->c >= low && a->d >= a->c)
return 3;
else if(a->d >= low && a->c >= low)
return 4;
else
return 5;
}
int cmp(const void *a, const void *b)
{
Stu *a1 = (Stu *)a, *b1 = (Stu *)b;
int sum = a1->c+a1->d, sum1 = b1->c+b1->d;
if(check(a1) == check(b1))
{
if(sum == sum1)
{
if(b1->d == a1->d)
return a1->id - b1->id;
else
return b1->d - a1->d;
}
else
return sum1-sum;
}
else
return check(a1)-check(b1);
}
int main(void)
{
int n = 0, count = 0, i = 0, j = 0;
scanf("%d %d %d", &n, &low, &high);
Stu *stu = (Stu *)malloc(sizeof(Stu)*n);
for(i = 0; i < n; i++)
{
scanf("%d %d %d", &stu[i].id, &stu[i].d, &stu[i].c);
count += stu[i].d >= low && stu[i].c >= low ? 1:0;
}
qsort(stu, n, sizeof(Stu), cmp);
printf("%d\n", count);
for(i = 0; i < count; i++)
printf("%d %d %d\n", stu[i].id, stu[i].d, stu[i].c);
return 0;
}
总结:这道题二点,一是:想到先划分类,二是:如何使用上qsort()函数。