最新消息: USBMI致力于为网友们分享Windows、安卓、IOS等主流手机系统相关的资讯以及评测、同时提供相关教程、应用、软件下载等服务。

HDU2673:shǎ崽 OrOrOrOrz

常识 admin 4浏览 0评论

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
  
  
 


快排的运用

 

#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;
}


 

 

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
  
  
 


快排的运用

 

#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;
}


 

 

发布评论

评论列表 (0)

  1. 暂无评论