POJ 2388.Who's in the Middle
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
* 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
5 2 4 1 3 5Sample Output
3Hint
INPUT DETAILS:
Five cows with milk outputs of 1..5
OUTPUT DETAILS:
1 and 2 are below 3; 4 and 5 are above 3.
对一组数求其中位数(连有两个中位数的情况都不用考虑)
直接排序即可
AC代码:GitHub
1 /* 2 By:OhYee 3 Github:OhYee 4 HomePage:http://www.oyohyee.com 5 Email:oyohyee@oyohyee.com 6 Blog:http://www.cnblogs.com/ohyee/ 7 8 かしこいかわいい? 9 エリーチカ! 10 要写出来Хорошо的代码哦~ 11 */ 12 13 #include <cstdio> 14 #include <algorithm> 15 #include <cstring> 16 #include <cmath> 17 #include <string> 18 #include <iostream> 19 #include <vector> 20 #include <list> 21 #include <queue> 22 #include <stack> 23 #include <map> 24 using namespace std; 25 26 //DEBUG MODE 27 #define debug 0 28 29 //循环 30 #define REP(n) for(int o=0;o<n;o++) 31 32 const int maxn = 10005; 33 int a[maxn]; 34 35 bool Do() { 36 int n; 37 if(scanf("%d",&n) == EOF) 38 return false; 39 40 REP(n) 41 scanf("%d",&a[o]); 42 43 sort(a,a + n); 44 45 printf("%d\n",a[n/2]); 46 47 return true; 48 } 49 50 int main() { 51 while(Do()); 52 return 0; 53 }
然而,我并不能保证我说的是对的。请自行验证,如有错误,请指正
新博客地址
https://www.oyohyee.com
https://www.oyohyee.com