UVA10107

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 problemyou 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 medianas 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 valuecannot split this array into equal two parts, so we consider the averageof the middle values {3,6}. Thus, the median will be (3+6)/2 = 4.5. Inthis 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 haveleading or trailing spaces.

Output 

For each input print the current value of the median.

SampleInput 

1346070502

SampleOutput 

12334274

题解:先排序,然后index为奇数时,取(1+index)/2;
                                          偶数是。取(a[index/2]+a[(index+1)/2])/2;
#include<iostream>
#include<algorithm>
using namespace std;
int a[10000];
int main()
{
	int x=0,i=1;
	while(cin>>x)
	{
		a[i++]=x;
	}
	for(int j=1;j<i;++j)
	{
		sort(a+1,a+j+1);
		if(j%2)
			cout<<a[(1+j)/2]<<endl;
		else
			cout<<(a[j/2]+a[j/2+1])/2<<endl;
	}

}
posted @ 2010-10-26 22:10  hailong  阅读(512)  评论(0编辑  收藏  举报