题解 | #整数奇偶排序#
整数奇偶排序
https://www.nowcoder.com/practice/bbbbf26601b6402c9abfa88de5833163
#include <iostream>
#include <algorithm>
using namespace std;
bool Compare(int x, int y) {
return x > y;
}
int main() {
int arr[10];
int odd = 0;
for (int& i : arr) {
cin >> i;
}
for (int i = 0; i < 10; ++i) {
if (arr[i] % 2 == 0) {
for (int j = i; j < 10; ++j) {
if (arr[j] % 2 != 0) {
odd++;
swap(arr[i], arr[j]);
break;
}
}
}else {
odd++;
}
}
sort(arr, arr + odd, Compare);
sort(arr + odd, arr + 10);
for (int i : arr) {
cout << i << " ";
}
}

查看14道真题和解析