题解 | #整数奇偶排序#
整数奇偶排序
http://www.nowcoder.com/practice/bbbbf26601b6402c9abfa88de5833163
sort()排序方法
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int arr[10];
bool cmp(int x, int y) {
/*三种情况:
1.同为奇数,返回大的
2.同为偶数,返回小的
3.奇偶相异,返回奇数
*/
if (x%2==1 && y%2==1)
return x > y;
else if (x%2==0 && y%2==0)
return x < y;
else if (x%2==1 && y%2==0)
return true;
else
return false;
}
int main() {
while (scanf("%d" ,&arr[0]) != EOF) {
for (int i=1; i<10; ++i) {
scanf("%d", &arr[i]);
}
sort ( arr, arr+10, cmp);
for (int i=0; i<10; i++)
printf("%d ", arr[i]);
printf("\n");
}
return 0;
}