首页 > 其他 > 详细

UVA10107 What is the Median?

时间:2014-07-03 17:44:13      阅读:457      评论:0      收藏:0      [点我收藏+]

What is the Median? 

The Problem

Median plays an important role in the world of statistics. By definition, it is a value which divides an array into two equal parts. In this problem you are to determine the current median of some long integers.

Suppose, we have five numbers {1,3,6,2,7}. In this case, 3 is the median as it has exactly two numbers on its each side. {1,2} and {6,7}.

If there are even number of values like {1,3,6,2,7,8}, only one value cannot split this array into equal two parts, so we consider the average of the middle values {3,6}. Thus, the median will be (3+6)/2 = 4.5. In this problem, you have to print only the integer part, not the fractional. As a result, according to this problem, the median will be 4!

Input 

The input file consists of series of integers X ( 0 <= X < 2^31 ) and total number of integers N is less than 10000. The numbers may have leading or trailing spaces.

Output 

For each input print the current value of the median.

Sample Input 

1
3
4
60
70
50
2

Sample Output 

1
2
3
3
4
27
4

Sadi Khan
2001-04-01

题意有些难读懂。

#include <cstdio>
#include <algorithm>
using std:: sort;
int arr[10002], id;

int main()
{
	int n;
	while(scanf("%d", &n) != EOF){
		arr[id++] = n;
		sort(arr, arr + id);
		
		if(id & 1) printf("%d\n", arr[id / 2]);
		else printf("%d\n", (arr[id / 2] + arr[id / 2 - 1]) / 2);
	}
	return 0;
}


UVA10107 What is the Median?,布布扣,bubuko.com

UVA10107 What is the Median?

原文:http://blog.csdn.net/chang_mu/article/details/36434677

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!