您的位置:首页 > 其它

HDOJ1157 Who's in the Middle(排序)

2014-07-24 15:10 323 查看

Who's in the Middle

[align=left]Problem Description[/align]
FJ is surveying his herd to find the most average cow. He wants to know how much milk this 'median' cow gives: half of the cows give as much or more than the median; half give as much or less.

Given an odd number of cows N (1 <= N < 10,000) and their milk output (1..1,000,000), find the median amount of milk given such that at least half the cows give the same amount of milk or more and at least half give the same or less.

 
[align=left]Input[/align]
* Line 1: A single integer N

* Lines 2..N+1: Each line contains a single integer that is the milk output of one cow.

 
[align=left]Output[/align]
* Line 1: A single integer that is the median milk output.

 
[align=left]Sample Input[/align]

5
2
4
1
3
5

 
[align=left]Sample Output[/align]

3

 
 

#include<stdio.h>

#include<algorithm>

using namespace std;

#define max 10000+5

int a[max];

int main()

{

    int T;

    int i;

    while(~scanf("%d",&T))

    {

      for(i=0;i<T;i++)

      {

        scanf("%d",&a[i]);

      }

      sort(a,a+T);

      if(T%2==0)  printf("%d\n",a[T/2]);

      else printf("%d\n",a[(T+1)/2-1]);

    }

    return 0;

}

       

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: