1117.整数奇偶排序

题目描述:

输入10个整数,彼此以空格分隔。重新排序以后输出(也按空格分隔),要求:
1.先输出其中的奇数,并按从大到小排列;
2.然后输出其中的偶数,并按从小到大排列。

输入:

任意排序的10个整数(0~100),彼此以空格分隔。

输出:

可能有多组测试数据,对于每组数据,按照要求排序后输出,由空格分隔。

样例输入:
4 7 3 13 11 12 0 47 34 98
样例输出:
47 13 11 7 3 0 4 12 34 98
提示:

1. 测试数据可能有很多组,请使用while(cin>>a[0]>>a[1]>>...>>a[9])类似的做法来实现;
2. 输入数据随机,有可能相等。

来源:

2008年北京大学图形实验室计算机研究生机试真题

#include<iostream>
#include<algorithm>
using namespace std;

bool compare(int a,int b){
        return a>b;
}

int main(){
    int a[11];
    int b[11];
    while(cin>>a[0]){
    int odd=0;
    if(a[0]%2==1) odd++;
    for(int i=1;i<10;i++){
        cin>>a[i];
        if(a[i]%2==1) odd++;
    }
    int p=odd;
    int q=odd;
    for(int i=0;i<10;i++){
        if(a[i]%2==1) b[--odd]=a[i];
    }
    for(int i=0;i<10;i++){
        if(a[i]%2==0) b[p++]=a[i];
    }
    sort(a,a+q,compare);
    sort(a+q+1,a+10);
    for(int i=0;i<10;i++){
        if(i==0) cout<<a[i];
        else cout<<" "<<a[i];
    }
    cout<<endl;
}
return 0;
}
原文地址:https://www.cnblogs.com/bernieloveslife/p/9735023.html