题解 | #整数奇偶排序#

整数奇偶排序

https://www.nowcoder.com/practice/bbbbf26601b6402c9abfa88de5833163

#include "cstdio"
#include "algorithm"

using namespace std;

bool comp(int left, int right) {
    return left > right;
}

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];
    }

    // 1 奇数
    int  begin = 0;
    for (int i = 0; i < n; ++i) {
        if (arr[i] % 2 == 1) {
            res[begin++] = arr[i];
        }
    }
    sort(res, res + begin, comp);
    for (int i = 0; i < begin; ++i) {
        printf("%d ", res[i]);
    }
//    printf("\n");

    // 2. 偶数
    begin = 0;
    for (int i = 0; i < n; ++i) {
        if (arr[i] % 2 == 0) {
            res[begin++] = arr[i];
        }
    }
    sort(res, res + begin);
    for (int i = 0; i < begin; ++i) {
        printf("%d", res[i]);
        if (i < begin + 1)
            printf(" ");
    }
    return 0;
}

全部评论

相关推荐

点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务