题解 | STL

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

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

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

int main() {
    int n;
    cin >> n;
    vector<int> data(n);
    for(int i = 0; i < n; ++i) {
        cin >> data[i];
    }
    int flag;
    cin >> flag;
    sort(begin(data), end(data));
    if (flag == 0) {
        for (int i = 0; i < n; ++i) {
            cout << data[i];
            if (i < n - 1) {
                cout << " ";
            } else {
                cout << endl;
            }
        }
    } else {
        for (int i = n - 1; i >= 0; --i) {
            cout << data[i];
            if (i > 0) {
                cout << " ";
            } else {
                cout << endl;
            }
        }
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

06-25 09:33
厦门大学 Java
程序员饺子:现在日常估计没啥hc了,等到八月多估计就慢慢有了。双九✌🏻不用焦虑的
投递快手等公司8个岗位
点赞 评论 收藏
分享
06-11 17:39
门头沟学院 Java
小呆呆的大鼻涕:卧槽,用户彻底怒了
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
06-30 18:19
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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