题解 | #整数奇偶排序#
整数奇偶排序
https://www.nowcoder.com/practice/bbbbf26601b6402c9abfa88de5833163
#include "cstdio" #include "algorithm" using namespace std; // comp 函数一次性按要求排序一个数组 bool comp3(int left, int right) { if (left % 2 == 1 && right % 2 == 0) { return true; } else if (left % 2 == 1 && right % 2 == 1 && left > right) { return true; } else if (left % 2 == 0 && right % 2 == 0 && left < right) { return true; } return false; } int main() { int n = 10; int arr[101]; int res[10]; for (int i = 0; i < n; ++i) { scanf("%d", &arr[i]); res[i] = arr[i]; } sort(arr, arr + 10, comp3); for (int i = 0; i < 10; ++i) { printf("%d ", arr[i]); } return 0; }