Skip to content

Instantly share code, notes, and snippets.

@henrybear327
Created March 19, 2015 12:34
Show Gist options
  • Save henrybear327/07b086114fdb9cdecbcd to your computer and use it in GitHub Desktop.
Save henrybear327/07b086114fdb9cdecbcd to your computer and use it in GitHub Desktop.
uva 10041.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int compare(const void *a, const void *b)
{
return *(int *)a - *(int *)b; /*positive, don't swap*/
}
int main()
{
int test_case;
while(scanf("%d", &test_case) != EOF) {
while(test_case--) {
int cases, data[500] = {0};
scanf("%d", &cases);
int i, sum = 0;
for(i= 0; i < cases; i++)
scanf("%d", &data[i]);
qsort(data, cases, sizeof(int), compare);
int total = 0, mid = cases / 2;
for(i = 0; i < cases; i++) {
total += abs(data[i] - data[mid]);
}
printf("%d\n", total);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment