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

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

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;
    }
}
全部评论

相关推荐

06-19 19:06
门头沟学院 Java
码农索隆:别去东软,真学不到东西,真事
点赞 评论 收藏
分享
评论
1
3
分享

创作者周榜

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