题解 | #输入整型数组和排序标识,对其元素按照进行排序#

输入整型数组和排序标识,对其元素按照升序或降序进行排序

https://www.nowcoder.com/practice/dd0c6b26c9e541f5b935047ff4156309

#include <iostream>
using namespace std;
#include <list>

bool sortU(int a,int b){
    if(a>b){
        return false;
    }
    return true;
}

bool sortD(int a,int b){
    if(a<b){
        return false;
    }
    return true;
}


int main() {
    int num,a,type;
    cin >> num;
    list<int> lst;
    for(int i = 0; i < num ;i++){
        cin >> a;
        lst.push_back(a);
    }
    cin >> type;
    if(type == 0){
        lst.sort(sortU);
    }
    else{
        lst.sort(sortD);
    }

    for(auto x : lst){
        cout << x <<" ";
    }
}

list和自定义排序函数的应用

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-07 18:05
哈哈哈哈哈感觉朋友找工作的已经疯掉了,直接上图
码农索隆:真老板娘:“我嘞个去,这不我当年的套路吗
点赞 评论 收藏
分享
07-01 13:37
门头沟学院 Java
steelhead:不是你的问题,这是社会的问题。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务