整数奇偶排序——比较函数sort

整数奇偶排序

https://www.nowcoder.com/practice/bbbbf26601b6402c9abfa88de5833163?tpId=40&&tqId=21398&rp=1&ru=/ta/kaoyan&qru=/ta/kaoyan/question-ranking

#include <iostream>
#include <cstdio>
#include <algorithm>
 
using namespace std;
int arr[10];

bool Compare(int x,int y){
    if(x%2==0&&y%2==0)
        return x<y;
    else if(x%2==1&&y%2==1)
        return  x>y;
    else if(x%2==1&&y%2==0)
        return true;
    else
        return false;
}

int main(){
    while(scanf("%d", &arr[0])!=EOF){        //输入10个整数,彼此以空格分隔,等于用十次EOF
        for(int i=1;i<10;i++){
            scanf("%d",&arr[i]);
        }
        sort(arr, arr+10,Compare);
        for(int i=0;i<10;i++)
            printf("%d ",arr[i]);        //对于每组数据,按照要求排序后输出,由空格分隔
    }
    return 0;
}

全部评论

相关推荐

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