文章目录
- 零、原题链接
- 一、题目描述
- 二、测试用例
- 三、解题思路
- 四、参考代码
零、原题链接
HJ101 输入整型数组和排序标识
一、题目描述
二、测试用例
三、解题思路
- 基本思路:
选择一个排序算法,然后根据标识确定升序还是降序; - 具体思路:
- 输入数据和排序标识
- 根据标识确定快排的顺序
- 输出结果
四、参考代码
时间复杂度:
O
(
n
l
o
g
n
)
\Omicron(nlog\; n)
O(nlogn)【快速排序时间复杂度】
空间复杂度:
O
(
l
o
g
n
)
\Omicron(log \; n)
O(logn)
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
cin >> n;
if (n == 1) {
sort(a.begin(), a.end(), greater<int>());
} else {
sort(a.begin(), a.end(), less<int>());
}
for (const auto& x : a) {
cout << x << ' ';
}
}
// 64 位输出请用 printf("%lld")