交叉排序

交叉排序
Time Limit: 1000MS Memory Limit: 32768KB
Submit Statistic Discuss
Problem Description

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

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

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

6
1 2 3 4 5 6
Example Output

1 6 3 4 5 2
Hint

Author

2011软件1-5班《程序设计基础》机试 tongjiantao

#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()
{
    int n;
    int a[100], e=0;
    int b[100], d=0;
    int i;
    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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值