题目
思路
个人认为这道题是前15道题中最难的一道;主要分为三步
-
录入数据并分类
使用一个结构数组来记录所有学生的信息,结构中有一个type成员来区分每个学生的类别,-1表示淘汰,0~3类考生的优先级依次降低(0类表示德才兼备,以此类推) -
对数据排序
(1)由于判断条件很多,写一个judge函数判断两个学生在打印时的先后顺序,这里优先按照type递增的顺序排序,打印时只需要跳过最前面type==-1的部分即可,其余元素的优先顺序按照题目中所给的写出
(2)一开始排序时采用了直接插入排序,有三个用例会超时,于是又依次尝试了希尔排序和快速排序,两种方式都可以,估计所有O(n^2)平方级别的排序方法应该都无法完成;代码中给出了三个排序函数,读者可以自行测试一下; -
打印数据
打印时先统计所有type==-1的数据数量,依次算出达到最低分数线的人数,然后打印剩下的所有元素即可
#include<stdio.h>
#include<stdlib.h>
typedef struct
{
int id;
int d;
int c;
int type;//-1表示淘汰,0~3类考生的优先级依次降低
}people;
int judge(people a, people b)//按照输出顺序,a在b之前则返回1,a在b之后则返回0;
{
if (a.type < b.type)
return 1;
else if (a.type == b.type)
if ((a.c + a.d) > (b.c + b.d))
return 1;
else if ((a.c + a.d) == (b.c + b.d))
if (a.d > b.d)
return 1;
else if (a.d == b.d)
if (a.id < b.id)
return 1;
else
return 0;
else
return 0;
else
return 0;
else
return 0;
}
void shellsort(people st[], int n)//希尔排序
{
int i, j, k;
for (i = n / 2; i >= 1; i /= 2)
{
for (j = i + 1; j <= n; j++)
if (judge(st[j], st[j - i]))
{
st[0] = st[j];
for (k = j - i; k > 0 && judge(st[0], st[k]); k = k - i)
st[k + i] = st[k];
st[k + i] = st[0];
}
}
}
void insertsort(people st[], int n)//直接插入排序
{
int i, j;
for (i = 2; i <= n; i++)
{
if (judge(st[i], st[i - 1]))
{
st[0] = st[i];
for (j = i - 1; judge(st[0], st[j]); j--)
st[j + 1] = st[j];
st[j + 1] = st[0];
}
}
}
int quick(people st[], int low, int high)//快速排序辅助
{
people temp = st[low];
while (low < high)
{
while (low < high && judge(temp, st[high]))
high--;
st[low] = st[high];
while (low < high && judge(st[low], temp))
low++;
st[high] = st[low];
}
st[low] = temp;
return low;
}
void quicksort(people st[], int low, int high)//快速排序
{
if (low < high)
{
int p = quick(st, low, high);
quicksort(st, low, p - 1);
quicksort(st, p + 1, high);
}
}
int main(void)
{
int n, l, h, i, j;
scanf("%d%d%d", &n, &l, &h);
people* st = (people*)malloc((n+1) * sizeof(people));
for (i = 1; i <= n; i++)
{
scanf("%d%d%d", &st[i].id, &st[i].d, &st[i].c);
if (st[i].d < l || st[i].c < l)
st[i].type = -1;
else if (st[i].d >= h && st[i].c >= h)
st[i].type = 0;
else if (st[i].d >= h)
st[i].type = 1;
else if (st[i].c < h && st[i].d >= st[i].c)
st[i].type = 2;
else
st[i].type = 3;
}
quicksort(st, 1, n);
for (i = 1; st[i].type == -1; i++);
printf("%d\n", n - i + 1);
for (j = i; j <= n; j++)
printf("%d %d %d\n", st[j].id, st[j].d, st[j].c);
return 0;
}