程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> HDU2673:shǎ崽 OrOrOrOrz

HDU2673:shǎ崽 OrOrOrOrz

編輯:C++入門知識

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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved