文章

17

粉丝

43

获赞

10

访问

8.8k

头像
整数奇偶排序 题解:
P1248 北京大学机试题
发布于2023年7月31日 09:48
阅读数 353

采用两个数组存放的方式,因为两次输出的大小顺序不同,所以采用同一个cmp的方式不可行,故根据奇数偶数分为两个数组进行存放,然后排序输出。

 

#include<bits/stdc++.h>
using namespace std;


int main(){
    int a[10];
    while(cin>>a[0]>>a[1]>>a[2]>>a[3]>>a[4]>>a[5]>>a[6]>>a[7]>>a[8]>>a[9])
    {
        int cntj=0,cnto=0;
        int j[10]={0};
        int o[10]={0};
        for(int i=0;i<10;i++)
        {
            if(a[i]%2==1)
            {
                j[cntj++]=a[i];
            }
            else o[cnto++]=a[i];
        }
        sort(j,j+cntj,greater<int>());
        for(int i=0;i<cntj;i++)
        {
            cout<<j[i]<<" ";
        }
        sort(o,o+cnto);
        for(int i=0;i<cnto;i++)
        {
            cout<<o[i]<<" ";
        }
        cout<<endl;

    }



	return 0;
}

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发