Follows 10041.c (Total 40 lines):

/* @JUDGE_ID:4461XX 10041 C "median->sum" */
/* A */
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define MAX 500

int data[MAX] , r ;
int sort_f( const void *a , const void *b )
{
	return *(int *)a - *(int *)b ;
}
int count( int num )
{
	int i , sum ;

	for( sum=i=0 ; i<r ; i++ )
		sum += abs( data[i]-num ) ;

	return sum ;
}
int main( void )
{
	int casetime , i ;
	
	scanf( "%d" , &casetime ) ;
	for( ; casetime ;casetime-- ){
		scanf( "%d" , &r ) ;

		for( i=0 ; i<r ; i++ ) scanf( "%d" , &data[i] ) ;
		qsort( (void * )data , r , sizeof( data[0] ) , sort_f ) ;
		if( r%2 ) /* odd */
			printf( "%d\n" , count( data[r/2+1 -1] ) ) ;
		else 
			printf( "%d\n" , count( ( data[ r/2 -1]+data[r/2+1 -1] )/2 ) ) ;
	}

	return 0 ;
}
/* @END_OF_SOURCE_CODE */

Back to statistics
Ya-Lin Huang (C)
huangyl@gmail.com