华为机试-输入整型数组和排序标识,对其元素按照升序或降序进行排序(较难)

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

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

问题描述

输入整型数组和排序标识,对其元素按照升序或降序进行排序(一组测试用例可能会有多组数据)
本题有多组输入,请使用while(cin>>)处理

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
static bool cmp(int a,int b){
    return a>b;
}
int main(){
    int num;
    bool flag;
    while(cin>>num){
        vector<int> input;
        while(num--){
            int a;
            cin>>a;
            input.push_back(a);
        }
        cin>>flag;
        if(flag)  sort(input.begin(),input.end(),cmp);
        else  sort(input.begin(),input.end());
        for(int i:input)
            cout<<i<<' ';
        cout<<endl;
    }
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-16 14:00
机械打工仔:来挂自己了,经典巨婴从校园投入职场
点赞 评论 收藏
分享
06-08 22:25
门头沟学院 Java
从零开始的转码生活:这hr不会打开手机不分青红皂白给所有人群发这句话,过一会再给所有人再发一遍,这肯定会有重复的,不管,再过一会再发一遍
点赞 评论 收藏
分享
评论
1
3
分享

创作者周榜

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