题解 | #整数奇偶排序#
整数奇偶排序
https://www.nowcoder.com/practice/bbbbf26601b6402c9abfa88de5833163
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 101;
bool cmp(int a, int b){//关于cmp函数,当返回true时,a, b不交换顺序,当返回false时,a, b交换顺序
if(a % 2 == 0 && b % 2 == 0)
return a < b;
else if(a % 2 != 0 && b % 2 != 0)
return a >b;
else if (a % 2 != 0 && b % 2 == 0)
return true;
else
return false;
}
int q[N];
int main() {
int n = 10;
while (cin >> q[0]) {
for(int i = 1; i < n; i ++)
cin >> q[i];
sort(q, q + n, cmp);
for(int i = 0; i < n; i ++)
cout << q[i] << " ";
}
return 0;
}
// 64 位输出请用 printf("%lld")

查看9道真题和解析