#这题主要用到了sort函数去简化排序的过程(需要声明#include <algorithm>)
描述
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.
输入
* Line 1: A single integer N
* Lines 2..N+1: Each line contains a single integer that is the milk output of one cow.
样例输入
5 2 4 1 3 5
样例输出
3
代码如下:
1 #include <iostream> 2 #include <algorithm> 3 4 using namespace std; 5 6 int main() 7 { 8 int i=0; 9 int cow[10000]; 10 while(scanf("%d",&i)!=EOF) 11 { 12 for(int s=0;s<i;s++) 13 { 14 cin>>cow[s]; 15 } 16 sort(cow,cow+i); 17 18 if(i%2==1) 19 { 20 i=i/2; 21 cout<<cow[i]<<endl; 22 } 23 else 24 { 25 i=i/2; 26 cout<<(cow[i-1]+cow[i])/2.0<<endl; 27 } 28 } 29 return 0; 30 }
ACM题目-Who's in the Middle
时间: 2024-10-10 02:54:29