交叉排序

来源:互联网 发布:如何提高淘宝宝贝排名 编辑:程序博客网 时间:2024/05/29 17:15

交叉排序
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;}
原创粉丝点击