题解 | #输入整型数组,对其元素按照升序或降序进行排序#
输入整型数组和排序标识,对其元素按照升序或降序进行排序
https://www.nowcoder.com/practice/dd0c6b26c9e541f5b935047ff4156309
#include <iostream> #include <vector> using namespace std; int main () { int n,num,Sequence; vector<int> vecNum; cin >> n; while (n--){ cin >> num; vecNum.push_back(num); } cin >> Sequence; int len = vecNum.size(); for (int i = 0; i < len; i++){ for (int j = i; j < len; j++){ if (Sequence){ if (vecNum[i] < vecNum[j]){ //降序 swap(vecNum[i],vecNum[j]); } } else{ if (vecNum[i] > vecNum[j]){ //升序 swap(vecNum[i],vecNum[j]); } } } } for (int i = 0; i < len; i++){ cout << vecNum[i] << " "; } cout << endl; return 0; }