首页 > ACM题库 > HDU-杭电 > hdu 2673 shǎ崽 OrOrOrOrz[解题报告]C++
2014
02-12

hdu 2673 shǎ崽 OrOrOrOrz[解题报告]C++

shǎ崽 OrOrOrOrz

问题描述 :

Acmer in HDU-ACM team are ambitious, especially shǎ崽, he can spend time in Internet bar doing problems overnight. So many girls want to meet and Orz him. But Orz him is not that easy.You must solve this problem first.
The problem is :
Give you a sequence of distinct integers, choose numbers as following : first choose the biggest, then smallest, then second biggest, second smallest etc. Until all the numbers was chosen .
For example, give you 1 2 3 4 5, you should output 5 1 4 2 3

输入:

There are multiple test cases, each case begins with one integer N(1 <= N <= 10000), following N distinct integers.

输出:

There are multiple test cases, each case begins with one integer N(1 <= N <= 10000), following N distinct integers.

样例输入:

5
1 2 3 4 5

样例输出:

5 1 4 2 3

#include <cstdio>
#include <algorithm>
using namespace std;
int main()
{
    int n;
    while (scanf("%d",&n)!=EOF)
    {
        int a[10010];
        for (int i=0; i<n; i++)
        scanf("%d",&a[i]);
        sort(a,a+n);
        int begin=0,end=n-2,flag=1;
        printf("%d",a[n-1]);
        while (begin<=end)
        {
            if (flag) {printf(" %d",a[begin]); begin++;}
            else {printf(" %d",a[end]); end--;}
            flag^=1;
        }
        printf("\n");   
    }
    return 0;
}

 

解题转自:http://www.cnblogs.com/forever97/archive/2013/12/24/3489660.html


  1. 第二个方法挺不错。NewHead代表新的头节点,通过递归找到最后一个节点之后,就把这个节点赋给NewHead,然后一直返回返回,中途这个值是没有变化的,一边返回一边把相应的指针方向颠倒,最后结束时返回新的头节点到主函数。