首页 > 专题系列 > Java解POJ > POJ 2623 Sequence Median [解题报告] Java
2013
11-11

POJ 2623 Sequence Median [解题报告] Java

Sequence Median

问题描述 :

Given a sequence of N nonnegative integers. Let’s define the median of such sequence. If N is odd the median is the element with stands in the middle of the sequence after it is sorted. One may notice that in this case the median has position (N+1)/2 in sorted sequence if sequence elements are numbered starting with 1. If N is even then the median is the semi-sum of the two “middle” elements of sorted sequence. I.e. semi-sum of the elements in positions N/2 and (N/2)+1 of sorted sequence. But original sequence might be unsorted.

Your task is to write program to find the median of given sequence.

输入:

The first line of input contains the only integer number N – the length of the sequence. Sequence itself follows in subsequent lines, one number in a line. The length of the sequence lies in the range from 1 to 250000. Each element of the sequence is a positive integer not greater than 2^32 – 1 inclusive.

输出:

You should print the value of the median with exactly one digit after decimal point.

样例输入:

4
3
6
4
5

样例输出:

4.5

温馨提示:

Huge input,scanf is recommended.

解题代码:

//* @author: 82638882@163.com
import java.io.*;
import java.util.*;
public class Main
{//1979
 public static void main(String[] args) throws IOException
 {
	InputStreamReader is=new InputStreamReader(System.in);
	BufferedReader in=new BufferedReader(is);
	int a=Integer.parseInt(in.readLine());
	int[] arr=new int[a+1];
	for(int i=1;i<=a;i++)
		arr[i]=Integer.parseInt(in.readLine());
	Arrays.sort(arr);
	double t;
	if(a%2==0)t=arr[a/2]/2.0+arr[a/2+1]/2.0;
	else t=arr[(a+1)/2];
	System.out.printf("%.1f",t);
  }
}

  1. 第2题,TCP不支持多播,多播和广播仅应用于UDP。所以B选项是不对的。第2题,TCP不支持多播,多播和广播仅应用于UDP。所以B选项是不对的。