From 57d767cf4e4d5b83645bda2c4e398fdd76d059e1 Mon Sep 17 00:00:00 2001 From: Raphael McSinyx Date: Mon, 2 Jan 2017 20:37:59 +0700 Subject: Update others/easy20160714 --- others/easy20160714/31.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 others/easy20160714/31.c (limited to 'others/easy20160714/31.c') diff --git a/others/easy20160714/31.c b/others/easy20160714/31.c new file mode 100644 index 0000000..4465da9 --- /dev/null +++ b/others/easy20160714/31.c @@ -0,0 +1,34 @@ +#include +#include + +int cmp(const void *x, const void *y) +{ + return *(int *) x - *(int *) y; +} + +int main() +{ + FILE *f = fopen("INP.TXT", "r"); + int n, *a, i, m; + long r = 0; + + fscanf(f, "%d", &n); + a = malloc(n * sizeof(int)); + + for (i = 0; i < n; i++) + fscanf(f, "%d", a + i); + + fclose(f); + + qsort(a, n, sizeof(int), cmp); + m = a[n / 2]; + + for (i = 0; i < n; i++) + r += abs(a[i] - m); + + f = fopen("OUT.TXT", "w"); + fprintf(f, "%ld\n", r); + fclose(f); + + return 0; +} -- cgit 1.4.1