UVA 10041
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=24&problem=982&mosmsg=Submission+received+with+ID+11407931
vito 在一条街选间房子,使得到亲戚家的距离之和最小,求出这个和,即在数组中求出中位数,计算各个数与中位数的差之和
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<stdio.h> #include<math.h> int main() { int c,a[555],n,i,j,temp,sum,avr; scanf("%d",&c); while(c--) { scanf("%d",&n); sum=0; for(i=0;i<n;i++) { scanf("%d",&a[i]); } for(j=1;j<n;j++) for(i=0;i<n-j;i++) if(a[i]>a[i+1]) { temp=a[i]; a[i]=a[i+1]; a[i+1]=temp; } if(n%2==0) avr=a[(n-1)/2]; else avr=a[n/2]; for(i=0;i<n;i++) sum+=fabs(avr-a[i]); printf("%d\n",sum); } return 0; }