题解 | #输入整型数组和排序标识,对其排序#暴力解法
输入整型数组和排序标识,对其元素按照升序或降序进行排序
https://www.nowcoder.com/practice/dd0c6b26c9e541f5b935047ff4156309
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { int n; vector<int> vec; while (cin >> n) { int temp; //得到数组 for (int i = 0; i < n; i++) { cin >> temp; vec.push_back(temp); } int flag; cin >> flag; //升序 if (flag == 0) { for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (vec[i] >= vec[j]) { temp = vec[i]; vec[i] = vec[j]; vec[j] = temp; } } } } //降序 if (flag == 1) { for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (vec[i] <= vec[j]) { temp = vec[i]; vec[i] = vec[j]; vec[j] = temp; } } } } } for (int i = 0; i < n; i++) { cout << vec[i] << ' '; } return 0; }