美文网首页
C++排序函数sort()

C++排序函数sort()

作者: 流年花影 | 来源:发表于2017-09-23 12:27 被阅读0次

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

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

    输入格式:

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

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

    输出格式:

    输出第1行首先给出达到最低分数线的考生人数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
    这道题本质就是根据不同的情况去排序,可是它复杂的地方就在于情况很多,所以我们可以先把不同的情况分出来,再去排序,但这道题的坑点在于它是限时的,200ms,这意味着我们必须精简排序算法,这就不得不提到C++一个强大的函数sort(),它的实现正是采用了快速排序的算法。我就简单介绍一下sort()的用法,举一个小例子:

    #include<iostream>
    #include <algorithm>//这个文件必须引入,太实现了sort()函数
    using namespace std;
    int main(){
        int a[5]={2,5,3,6,9};
        sort(a,a+5);    //sort()对数组进行排序,两个参数分别是数组开始和结束的位置 
        for(int i=0;i<5;i++){
            cout<<a[i]<<" ";
        }
        return 0;
    } 
    

    结果:2 3 5 6 9
    我们可以看到sort()函数默认升序排序,那如果实际应用中需要升序呢,其实sort()函数还可以通过传递排序方式的函数进行排序,继续举例:

    #include<iostream>
    #include <algorithm>//这个文件必须引入,太实现了sort()函数
    using namespace std;
    bool compare(int a,int b){
        if(a>b){
            return true;
        }else{
            return false;
        }
    }
    int main(){
        int a[5]={2,5,3,6,9};
        sort(a,a+5,compare);    //sort()对数组进行排序,前两个参数分别是数组开始和结束的位置,后一个参数就是你自己定义的排序函数 
        for(int i=0;i<5;i++){
            cout<<a[i]<<" ";
        }
        return 0;
    } 
    

    结果:9 6 5 3 2
    这个在实际应用中就有很大的用处,以此题为例,我们可以定义结构体数组,存放不同同学的成绩,然后利用sort()排序函数,传入成绩数组,再传入我们自己定义的排序函数,可以很轻松的解决这个题目,代码如下:

    #include<iostream>
    #include<stdio.h>
    #include <algorithm>
    #include <vector>
    using namespace std;
    struct node {
        int num;
        int de;
        int cai;
    };
    int cmp(struct node a, struct node b) {
        if ((a.de + a.cai) != (b.de + b.cai))
            return (a.de + a.cai) > (b.de + b.cai);
        else if (a.de != b.de)
            return a.de > b.de;
        else
            return a.num < b.num;
    }
    int main() {
        int n, low, high;
        scanf("%d %d %d", &n, &low, &high);
        vector<node> v[4];
        node temp;
        int total = n;
        for (int i = 0; i < n; i++) {
            scanf("%d %d %d", &temp.num, &temp.de, &temp.cai);
            if (temp.de < low || temp.cai < low)
                total--;
            else if (temp.de >= high && temp.cai >= high)
                {
                v[0].push_back(temp);
                }
            else if (temp.de >= high && temp.cai < high)
                v[1].push_back(temp);
            else if (temp.de < high && temp.cai < high && temp.de >= temp.cai)
                v[2].push_back(temp);
            else
                v[3].push_back(temp);
        }
        printf("%d\n", total);
        for (int i = 0; i < 4; i++) {
            sort(v[i].begin(), v[i].end(), cmp);
            for (int j = 0; j < v[i].size(); j++)
                printf("%d %d %d\n", v[i][j].num, v[i][j].de, v[i][j].cai);
        }
        return 0;
    }
    

    相关文章

      网友评论

          本文标题:C++排序函数sort()

          本文链接:https://www.haomeiwen.com/subject/ulkcextx.html