Computer Programming Contest Preparation

ToolBox - Source for: 101/10107/a.c



/home/toolbox/public_html/solutions/101/10107/a.c
    1 #include <stdio.h>
    2 #include <string.h>
    3 #include <sys/types.h>
    4 #include <sys/stat.h>
    5 #include <fcntl.h>
    6 #include <stdint.h>
    7 #include <math.h>
    8 #include <stdlib.h>
    9 
   10 #define TRUE  (1 == 1)
   11 #define FALSE (1 != 1)
   12 
   13 #define DEBUG if (FALSE)
   14 
   15 #define MAX_VALUES 10001
   16 
   17 /*
   18  *  Author: Isaac Traxler
   19  *    Date: 2015-03-09
   20  * Purpose:
   21  * Problem: 10107
   22  */
   23 
   24 /*
   25  * This template reads lines of data at a time until end of file.
   26  */
   27 
   28 int ary[MAX_VALUES];
   29 int cnt = 0;
   30 int t;
   31 
   32 void init()
   33 {
   34     /* FUNCTION init */
   35 } /* FUNCTION init */
   36 
   37 void dump()
   38 {
   39     /* FUNCTION dump */
   40     int i;
   41 
   42     printf("%d (", cnt);
   43     for (i=0; i<cnt; i++)
   44         {
   45             printf("%d ", ary[i]);
   46         }
   47     printf(")\n");
   48 } /* FUNCTION dump */
   49 
   50 int compare(const void *a, const void *b)
   51 {
   52     /* FUNCTION compare */
   53     return ( *(int*)a - *(int*)b );
   54 } /* FUNCTION compare */
   55 
   56 int getInput()
   57 {
   58     /* FUNCTION getInput */
   59     int dataReadFlag;
   60 
   61     dataReadFlag = 1 == scanf(" %d ", &t);
   62     return (dataReadFlag);
   63 } /* FUNCTION getInput */
   64 
   65 void process()
   66 {
   67     /* FUNCTION process */
   68     int m;
   69 
   70     ary[cnt] = t;
   71     m = cnt / 2;
   72     cnt++;
   73     DEBUG dump();
   74     qsort(ary, cnt, sizeof(int), compare);
   75     DEBUG dump();
   76     if (0 == (cnt % 2))
   77         {
   78             /* even */
   79             printf("%d\n", (ary[m] + ary[m+1]) / 2);
   80         } /* even */
   81     else
   82         {
   83             /* odd */
   84             printf("%d\n", ary[m]);
   85         } /* odd */
   86 } /* FUNCTION process */
   87 
   88 int main()
   89 {
   90     /* main */
   91     int moreToDo;
   92 
   93     init();
   94     moreToDo = getInput();
   95     while (moreToDo)
   96         {
   97             /* while */
   98             process();
   99             moreToDo = getInput();
  100         } /* while */
  101 
  102     return EXIT_SUCCESS;
  103 } /* main */
  104