一目了然的题解
整数奇偶排序
https://www.nowcoder.com/practice/bbbbf26601b6402c9abfa88de5833163
#include <iostream>
#include <algorithm>
using namespace std;
bool compare(int x, int y) {
if (x % 2 == 1 && y % 2 == 1) {
return x > y;
} else if (x % 2 == 0 && y % 2 == 0) {
return x < y;
} else {
return x % 2 > y % 2;//奇数在前
}
}
int main() {
int arr[10];
for (int i = 0; i < 10; ++i) {
cin >> arr[i];
}
sort(arr, arr + 10, compare);
for(int i = 0; i < 10; ++i){
std::cout<<arr[i]<<" ";
}
}
腾讯成长空间 5981人发布
