/* @问题:N个点的最短距离和 @Time:20160415 @Author:Alex */ #include <stdio.h> #include <math.h> #include <stdlib.h> void sort(int *string,int n); int main() { int M; int N; int count; int Minddle_val = 0; int Sumdistance = 0; int *Num = NULL; while(scanf("%d",&M) != EOF) { while(M--) { Sumdistance = 0; scanf("%d",&N); Num = (int *)malloc(N*sizeof(int)); if( NULL == Num) { exit(1); } for(count = 0;count < N; count++) { scanf("%d",Num+count); } sort(Num,N); Minddle_val = *(Num+(N/2)); for(count = 0;count < N; count++) { Sumdistance += abs(*(Num+count)-Minddle_val) ; } printf("%d\n",Sumdistance); free(Num); } } return 0; } //对数进行排序 void sort(int *string,int n) { int i=0,j=0; int temp=0; for(i=0;i<n-1;i++) { for(j=i+1;j<n;j++) { if(*(string+i)<*(string+j)) { temp = *(string+j); *(string+j) = *(string+i); *(string+i) = temp; } } } }