[解题报告]10107 - What is the Median?

本文介绍了一个通过不断接收整数输入并实时计算并输出当前中位数的程序实现方案。该程序能够处理多达10000个整数,并在每次输入后更新中位数。文章提供了一个具体的C语言实现示例。
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

 

关键句:For each input print the current value of the median.

 

略水

#include<stdio.h>
int main()
{
  int N[10005],top=0,temp;
  int i,j;
  while(scanf("%d",&N[top++])!=EOF)
  {
    for(i=0;i<top;i++)
      if(N[top-1]> N[i])
      {
        temp=N[top-1];
        for(j=top-1;j>i;j--) N[j]=N[j-1];
        N[i]=temp;
        break;
      }
    if(top%2)
      printf("%d\n",N[top/2]);
    else
      printf("%d\n",(N[top/2]+N[top/2-1])/2);
  }
  return 0;
}

 

转载于:https://www.cnblogs.com/TheLaughingMan/archive/2013/02/24/2924735.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值