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

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

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

思路

1.按要求接收数据
2.按要求排序
3.输出

注意事项

1.最后输出要记得换行

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int num;
    while(cin>>num)
    {
        vector<int> array;
        for(int i=0;i<num;i++)
        {
            int get;
            cin>>get;
            array.push_back(get);
        }
        int sor;
        cin>>sor;//接收0或1
        if(sor)//接收到1,从大到小
        {
            sort(array.begin(),array.end(),greater<int>());//从大到小排序用greater
        }
        else//接收到0,从小到大
        {
            sort(array.begin(),array.end(),less<int>());//从大到小排序用less
        }
        for(auto a:array)
            cout<<a<<" ";
        cout<<endl;//最后输出要记得换行
    }
    return 0;
}
全部评论

相关推荐

点赞 评论 收藏
转发
投递阿里巴巴控股集团等公司10个岗位 >
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务