HJ101题解 | #输入整型数组,进行排序#
输入整型数组和排序标识,对其元素按照升序或降序进行排序
https://www.nowcoder.com/practice/dd0c6b26c9e541f5b935047ff4156309
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int n;
int b;
cin >> n;
vector<int> vec;
while (n--) {
int a;
cin >> a;
vec.push_back(a);
}
cin >> b;
if (b) {
sort(vec.begin(), vec.end(), greater<int>());
} else {
sort(vec.begin(), vec.end());
}
for (auto x : vec) {
cout << x << " ";
}
cout << endl;
}
1.acm输入处理
2.理解使用sort函数 默认升序 如何降序 sort(begin,end,greater<type>());
