数据结构之 内部排序---交叉排序(没啥特别的!!!)

交叉排序

Time Limit: 1000MS Memory limit: 32768K

题目描述

输入N个数,把所有奇数位置上的数从小到大排序,把偶数位置上的数从大到小排序。

输入

输入的第一行是一个正整数N(2<=N<=100)。
第二行是N个用空格隔开的整数。

输出

输出只有一行N个数,是按要求排序后的序列,用空格隔开。

示例输入

6
1 2 3 4 5 6

示例输出

1 6 3 4 5 2


#include <iostream>
#include <string>
#include <stdio.h>
#include <string.h>
#include <algorithm>

using namespace std;

int cmp(int a, int b)
{
    return a>b;
}

int main()
{
    unsigned int n;
    int a[100], e=0;
    int b[100], d=0;
    int i, j;
    cin>>n;
    int dd;

    for(i=1; i<=n; i++)
    {
        cin>>dd;
        if(i%2==1)
          a[e++]=dd;
        else
          b[d++]=dd;
    }
    sort(a, a+e);
    sort(b, b+d, cmp);

    if(n%2==0)
    {
        for(i=0; i<(n/2); i++)
        {
            if(i==0)
              cout<<a[i]<<" "<<b[i];
            else
              cout<<" "<<a[i]<<" "<<b[i];
        }
        cout<<endl;
    }
    else
    {
        for(i=0; i<(n/2); i++)
        {
              cout<<a[i]<<" "<<b[i]<<" ";
        }
        cout<<a[e-1]<<endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/yspworld/p/4109286.html