题解 | 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")