码迷,mamicode.com
首页 > 其他好文 > 详细

SDAU课程练习--problemQ(1016)

时间:2016-04-04 22:37:36      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:

题目描述

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
5

Sample Output

3

题目大意

十足的水题,排序后找出中位数即可

AC代码

  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<algorithm>
  4. using namespace std;
  5. int a[10000];
  6. int main()
  7. {
  8. //freopen("date.in", "r", stdin);
  9. //freopen("date.out", "w", stdout);
  10. int N;
  11. while (~scanf("%d", &N))
  12. {
  13. //memset(a, 0, 10000 * sizeof(int));
  14. for(int i = 0; i < N; i++)
  15. {
  16. cin>>a[i];
  17. }
  18. sort(a, a + N);
  19. cout << a[(N + 1) / 2-1];
  20. }
  21. }




SDAU课程练习--problemQ(1016)

标签:

原文地址:http://www.cnblogs.com/liuzhanshan/p/5353010.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!