Problem P

来源:互联网 发布:阿拉伯血钻野燕麦知乎 编辑:程序博客网 时间:2024/05/01 03:02
Problem Description
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.
 

Input
* Line 1: A single integer N< br>< br>* Lines 2..N+1: Each line contains a single integer that is the milk output of one cow.
 

Output
* Line 1: A single integer that is the median milk output.
 

Sample Input
524135
 

Sample Output
3
 简单题意:
  奶牛的产量不尽相同,FJ想知道处于中位数的奶牛的产量。所以,写一个算法,求出中位数。
解题思路形成过程:
  这是一个很简单的题目,而且只需要考虑奶牛的个数是奇数就可以。所以算法特别容易。
感想:
  开始一直是Runtime Error,原因是数组的界限太小。最终改变了数组的大小,Accepted.
AC代码:
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
    int n,a[10000];
        while(cin>>n)
        {
            for(int i=1;i<=n;i++)
                cin>>a[i];
            sort(a+1,a+n+1);
            if(n%2==0)
            cout<<(a[n/2]+a[n/2+1])/(2*1.0)<<endl;
            else cout<<a[(n+1)/2]<<endl;
        }
    return 0;
    }
0 0
原创粉丝点击