C语言 —— PAT 【1015. 德才论 (25分)】 (运行超时——快速排序)

25 篇文章 4 订阅
15 篇文章 2 订阅

一、题目:

宋代史学家司马光在《资治通鉴》中有一段著名的“德才论”:“是故才德全尽谓之圣人,才德兼亡谓之愚人,德胜才谓之君子,才胜德谓之小人。凡取人之术,苟不得圣人,君子而与之,与其得小人,不若得愚人。”

现给出一批考生的德才分数,请根据司马光的理论给出录取排名。

输入格式:

输入第一行给出 3 个正整数,分别为:N(≤10​5​​),即考生总数;L(≥60),为录取最低分数线,即德分和才分均不低于 L 的考生才有资格被考虑录取;H(<100),为优先录取线——德分和才分均不低于此线的被定义为“才德全尽”,此类考生按德才总分从高到低排序;才分不到但德分到线的一类考生属于“德胜才”,也按总分排序,但排在第一类考生之后;德才分均低于 H,但是德分不低于才分的考生属于“才德兼亡”但尚有“德胜才”者,按总分排序,但排在第二类考生之后;其他达到最低线 L 的考生也按总分排序,但排在第三类考生之后。

随后 N 行,每行给出一位考生的信息,包括:准考证号 德分 才分,其中准考证号为 8 位整数,德才分为区间 [0, 100] 内的整数。数字间以空格分隔。

输出格式:

输出第一行首先给出达到最低分数线的考生人数 M,随后 M 行,每行按照输入格式输出一位考生的信息,考生按输入中说明的规则从高到低排序。当某类考生中有多人总分相同时,按其德分降序排列;若德分也并列,则按准考证号的升序输出。

输入样例:

14 60 80
10000001 64 90
10000002 90 60
10000011 85 80
10000003 85 80
10000004 80 85
10000005 82 77
10000006 83 76
10000007 90 78
10000008 75 79
10000009 59 90
10000010 88 45
10000012 80 100
10000013 90 99
10000014 66 60

输出样例:

12
10000013 90 99
10000012 80 100
10000003 85 80
10000011 85 80
10000004 80 85
10000007 90 78
10000006 83 76
10000005 82 77
10000002 90 60
10000014 66 60
10000008 75 79
10000001 64 90

 

 


 

二、结果:

 


 

三、问题:

最开始遇到运行超时就想到是排序算法不够优化,先用的是选择排序,再用的是冒泡(想着如果后面如果都是有序的,可以设置flag跳过后面的循环),最后才采用快速排序算法,也算是真正意义上体会到数据量特别大的时候用快速排序的优势。

(第一次运行快速排序算法的时候,发生了堆栈溢出,可以参考 这个 )

 


 

四、回顾快速排序算法:

原理看这里

//升序
void qsort(int a[], int low, int high)
{
    int i, j, temp;

    if(low < high)
    {
        i = low;
        j = high;
        temp = a[i];


        while(i < j)
        {
            while(i < j && a[j] > temp)
                j--;

            if(i < j)
            {
                a[i] = a[j];
                i++;
            }



            while(i < j && a[i] < temp)
                i++;

            if(i < j)
            {
                a[j] = a[i];
                j--;
            }

        }


        a[i] = temp;

        qsort(a, low, i-1);
        qsort(a, i+1, high);

    }
}

 


 

五、代码:

#include<stdio.h>


struct student {
    int num;
    int de;
    int cai;
};


int cmp(struct student stu_i, struct student stu_j);
void qsort(struct student stus[], int low, int high);

int main(void)
{
    int n, H, L;

    scanf("%d%d%d", &n, &L, &H);
    struct student stu_all[100000];

    for (int i = 0; i < n; i++)
    {
        struct student stu;
        scanf("%d%d%d", &stu.num, &stu.de, &stu.cai);
        stu_all[i] = stu;
    }

    struct student stu1[100000];
    struct student stu2[100000];
    struct student stu3[100000];
    struct student stu4[100000];

    int count1 = 0, count2 = 0, count3 = 0, count4 = 0;

    for (int i = 0; i < n; i++)
    {
        if (stu_all[i].de >= H && stu_all[i].cai >= H)
        {
            stu1[count1] = stu_all[i];
            count1++;
        }
        else if (stu_all[i].de >= H && stu_all[i].cai < H && stu_all[i].cai >= L)
        {
            stu2[count2] = stu_all[i];
            count2++;
        }
        else if (stu_all[i].de < H && stu_all[i].cai < H && stu_all[i].de >= stu_all[i].cai && stu_all[i].de >= L && stu_all[i].cai >= L)
        {
            stu3[count3] = stu_all[i];
            count3++;
        }
        else if (stu_all[i].de >= L && stu_all[i].cai >= L)
        {
            stu4[count4] = stu_all[i];
            count4++;
        }
        else
        {
            continue;
        }
    }

    printf("%d\n", count1 + count2 + count3 + count4);

    qsort(stu1, 0, count1 - 1);
    qsort(stu2, 0, count2 - 1);
    qsort(stu3, 0, count3 - 1);
    qsort(stu4, 0, count4 - 1);

    for (int i = 0; i < count1; i++)
    {
        printf("%d %d %d\n", stu1[i].num, stu1[i].de, stu1[i].cai);
    }
    for (int i = 0; i < count2; i++)
    {
        printf("%d %d %d\n", stu2[i].num, stu2[i].de, stu2[i].cai);
    }
    for (int i = 0; i < count3; i++)
    {
        printf("%d %d %d\n", stu3[i].num, stu3[i].de, stu3[i].cai);
    }
    for (int i = 0; i < count4; i++)
    {
        printf("%d %d %d\n", stu4[i].num, stu4[i].de, stu4[i].cai);
    }

}



int cmp(struct student stu_i, struct student stu_j)
{
    if (stu_i.de + stu_i.cai > stu_j.de + stu_j.cai)
    {
        return 1;
    }
    else if ((stu_i.de + stu_i.cai == stu_j.de + stu_j.cai) && stu_i.de > stu_j.de)
    {
        return 1;
    }
    else if ((stu_i.de + stu_i.cai == stu_j.de + stu_j.cai) && (stu_i.de == stu_j.de) && stu_i.num < stu_j.num)
    {
        return 1;
    }
    return 0;
}


void qsort(struct student stus[], int low, int high) 
{
    int i, j;
    struct student temp;

    if (low < high) 
    {
        i = low, j = high, temp = stus[i];

        while (i < j) 
        {
            while (i < j && !cmp(stus[j], temp))
                j--;
            if (i < j) {
                stus[i] = stus[j];
                i++;
            }

            while (i < j && cmp(stus[i], temp))
                i++;
            if (i < j) {
                stus[j] = stus[i];
                j--;
            }
        }
        stus[i] = temp;

        qsort(stus, low, i - 1);
        qsort(stus, i + 1, high);
    }
}

 

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值