HDACM2083
先把距离从小到大排序,然后取数组中间那个值为起点即可算出最小距离
import java.util.Arrays;
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int m = sc.nextInt();
while(m-->0){
int n = sc.nextInt();
int[]friend = new int[n];
for (int i = 0; i < friend.length; i++) {
friend[i] = sc.nextInt();
}
Arrays.sort(friend);
int sum = 0;
for (int i = 0; i < friend.length; i++) {
sum += Math.abs(friend[n/2]-friend[i]);
}
System.out.println(sum);
}
}
}