当前位置:编程学习 > C/C++ >>

HDU2673:shǎ崽 OrOrOrOrz

Problem Description
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
 
 
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
 
 
Sample Output
5 1 4 2 3
 
 
快排的运用
 
 
 
[cpp]  
#include <iostream>  
#include <cstdio>  
#include <cstring>  
#include <algorithm>  
using namespace std;  
  
int cmp(const void *a,const void *b)  
{  
    return (*(int *)a-*(int*)b);  
}  
  
int main()  
{  
    int n,i,num[10005],a[10005];  
    while(cin >> n)  
    {  
        int flag,flag2;  
        flag = n/2;  
        flag2 = n-flag;  
        memset(a,0,sizeof(a));  
        memset(num,0,sizeof(num));  
        for(i = 0;i<n;i++)  
        cin >> num[i];  
        qsort(num,n,sizeof(int),cmp);  
        int k = 0;  
        for(i = n-1;flag2--;i--)  
        {  
            a[k] = num[i];  
            k+=2;  
        }  
        k = 1;  
        for(i = 0;flag--;i++)  
        {  
            a[k] = num[i];  
            k+=2;  
        }  
        cout << a[0];  
        for(i = 1;i<n;i++)  
        {  
            cout << " " << a[i];  
        }  
        cout << endl;  
    }  
  
    return 0;  
}  
 
 
 
 
补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,