zcmu-4953: 整数奇偶排序(水)

 

Time Limit: 1 Sec  Memory Limit: 32 MB
Submit: 4  Solved: 4
[Submit][Status][Web Board]

Description

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

Input

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

Output

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

Sample Input

0 56 19 81 59 48 35 90 83 75 17 86 71 51 30 1 9 36 14 16

Sample Output

83 81 75 59 35 19 0 48 56 90 71 51 17 9 1 14 16 30 36 86

因为还要处理输出的空格,所以就把排序后的十个数整合到一个数组中去好了。 

#include <iostream>
#include <cstdio>
using namespace std;
const int maxn = 100 + 5;
bool cmp(int a,int b)
{
    return a > b;
}
int main()
{
    int a[10],b[10];
    int even[10];
    int odd[10];
    while(cin>>a[0]>>a[1]>>a[2]>>a[3]>>a[4]>>a[5]>>a[6]>>a[7]>>a[8]>>a[9])
    {
        int tmp = 0, tmp2 = 0,c = 0;
        for(int i = 0 ; i < 10;i++)
        {
            if(a[i] % 2 == 0)
                even[tmp++] = a[i];
            else
                odd[tmp2++] = a[i];
        }
        sort(odd,odd + tmp2,cmp);
        sort(even,even +tmp);
        for(int i = 0 ;i < tmp2;i++)
            b[c++] = odd[i];
        for(int i = 0 ; i < tmp;i++)
            b[c++] = even[i];
        for(int i = 0;i < c;i++)
        {
            if(!i)
                printf("%d",b[i]);
            else
                printf(" %d",b[i]);
        }
        printf("\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/hzyhfxt/article/details/82526317
今日推荐