Hdoj 2673 shǎ崽 OrOrOrOrz

来源:互联网 发布:怎么加入淘宝类目群 编辑:程序博客网 时间:2024/05/01 23:43

Problem Description Acmer in HDU-ACM team are ambitious, especiallyshǎ崽, 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 th 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

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

Output Output a sequence of distinct integers described above.

Sample Input

5 1 2 3 4 5

题目分析:
输入数据后排序一次再设置2个指针和一个flag即可交替输出
Code

#include<iostream>#include<cstdio>#include<algorithm>#include<cmath>#include<cctype>#include<cstring>#include<cstdlib>using namespace std;const int maxn=10001;int a[maxn];int main() {    int n;    while(cin>>n)    {        for(int i=1;i<=n;i++) cin>>a[i];        sort(a+1,a+n+1);        bool flag=true;        int p1=0;int p2=n+1;        while(n--)        {            if(!flag)            {                p1++;                cout<<a[p1];                if(n) cout<<" ";                flag= true;            }else            {                p2--;                cout<<a[p2];                if(n) cout<<" ";                flag= false;            }        }        cout<<endl;    }    return 0;}
原创粉丝点击