题解 | #输入整型数组和排序标识,对其元素按照进行排序#
输入整型数组和排序标识,对其元素按照升序或降序进行排序
https://www.nowcoder.com/practice/dd0c6b26c9e541f5b935047ff4156309
#include <iostream> using namespace std; #include <list> bool sortU(int a,int b){ if(a>b){ return false; } return true; } bool sortD(int a,int b){ if(a<b){ return false; } return true; } int main() { int num,a,type; cin >> num; list<int> lst; for(int i = 0; i < num ;i++){ cin >> a; lst.push_back(a); } cin >> type; if(type == 0){ lst.sort(sortU); } else{ lst.sort(sortD); } for(auto x : lst){ cout << x <<" "; } }
list和自定义排序函数的应用