andy6804tw
7/15/2016 - 10:56 AM

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=982 困難度 ★ 運算排序存取中位數並求出中位數到任何一親戚家的距離

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=982 困難度 ★ 運算排序存取中位數並求出中位數到任何一親戚家的距離

import java.util.Arrays;
import java.util.Scanner;
 
public class Main {
 
 public static void main(String[] args) {
  Scanner scn=new Scanner(System.in);
  int n=scn.nextInt(),num;
  for(int i=0;i<n;i++){
   int r=scn.nextInt(),arr[]=new int [r],tot=0;
   for(int j=0;j<r;j++)
    arr[j]=scn.nextInt();
       Arrays.sort(arr);
       if(r%2==0)num=(arr[r/2]+arr[r/2-1])/2;
       else num=arr[r/2];
       for(int j=0;j<r;j++)
        tot+=Math.abs(num-arr[j]);
       System.out.println(tot);
  }
 
 }
/* 
    題目:Q10041: Vito's family
    作者:1010
    時間:西元 2016 年 7 月 */
 
}