交叉排序
Time Limit: 1000MS Memory Limit: 32768KB
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 <bits/stdc++.h>
using namespace std;
int main()
{
int a[1211];
memset(a, 0, sizeof(a));
int n, temp;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
for(int i=1;i<=n;i++)
{
for(int j=i+2;j<=n;j+=2)
{
if((i%2==0&&a[i]<a[j])||(i%2==1&&a[i]>a[j]))
{
temp = a[i];
a[i] = a[j];
a[j] =temp;
}
}
}
for(int i=1;i<=n;i++)
{
if(i==1)
cout<<a[i];
else
cout<<" "<<a[i];
}
cout<<endl;
return 0;
}
/***************************************************
User name:
Result: Accepted
Take time: 0ms
Take Memory: 208KB
Submit time:
****************************************************/