南陽理工ACM 題目124 中位數

中位數

時間限制:3000 ms  |  內存限制:65535 KB
難度:2
     描述

一組數據按從小到大的順序依次排列,處在中間位置的一個數叫做中位數。

比如 1 5 10 11 9  其中位數就是9.因爲排序過後,9處在中間位置。

現在給你一些數,請你求出其中位數。

     輸入

第一行輸入一個整數T(1<=T<=1000)表示測試數據的組數。
隨後的一行是一個奇數M,表示該組測試數據中共有M(1<=M<=1000)個數。
隨後的一行有M個互不相同的整數,這些整數都不大於10000且不小於-10000。

     輸出

對於每組測試數據輸出一個整數,表示這M個數的中位數。

     樣例輸入
     1

     5

     1    5    10    11    9

     樣例輸出

     9

 
import java.io.*;
import java.util.*;

public class Main {

	public static int getN(int x){
		if(x==1)
			return 1;
		else
		return getN(x-1)+x;
	}
	
	public static void main(String[] args) {
				Scanner cin = new Scanner(new BufferedInputStream(System.in));
				int T = cin.nextInt();
				while(T-->0){
					int n = cin.nextInt();
					int[] arr = new int[n];
					for(int i=0;i<n;i++){
						arr[i] = cin.nextInt();
					}
					Arrays.sort(arr);
					System.out.println(arr[(n-1)/2]);
				}
				
		}
}        

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章