diff options
Diffstat (limited to '7.c')
-rw-r--r-- | 7.c | 78 |
1 files changed, 78 insertions, 0 deletions
@@ -0,0 +1,78 @@ +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + +static char *ReadLine(char *p, size_t size, FILE *fp) +{ + if ((p=fgets(p, size, fp))) + { + size_t l = strlen(p); + + while(l && p[l-1] == '\n') + { + p[--l] = 0; + } + } + + return p; +} + +static int Distance(int from, int to) +{ + return abs(from - to); +} + +int main(void) +{ + char buff[0x8000]; + int *num = NULL; + int num_count = 0; + char *p = NULL; + int min = 2147483647; + int max = 0; + int f = 0; + int n = 0; + int lowest = 2147483647; + + ReadLine(buff, sizeof buff, stdin); + + p = strtok(buff, ","); + + while(p) + { + num = realloc(num, (num_count + 1) * sizeof *num); + f = atoi(p); + + if (f > max) + { + max = f; + } + + if (f < min) + { + min = f; + } + + num[num_count++] = f; + p = strtok(NULL, ","); + } + + for(f = min; f <= max; f++) + { + int result = 0; + + for(n = 0; n < num_count; n++) + { + result += Distance(f, num[n]); + } + + if (result < lowest) + { + lowest = result; + } + } + + printf("lowest = %d\n", lowest); + + return 0; +} |