题解 | #整数奇偶排序#

整数奇偶排序

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;
}
全部评论

相关推荐

后来123321:别着急,我学院本大二,投了1100份,两个面试,其中一个还是我去线下招聘会投的简历,有时候这东西也得看运气
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务