Computer Programming Contest Preparation

ToolBox - Source for: 5/591/a.c



/home/toolbox/public_html/solutions/5/591/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 <stdlib.h>
    7 #include <math.h>
    8 #include <stdint.h>
    9 
   10 #define TRUE  (1 == 1)
   11 #define FALSE (1 != 1)
   12 
   13 #define DEBUG if (FALSE)
   14 #define MAX_COLS 51
   15 
   16 /* fprintf(stderr, "functionName: message", varslist); */
   17 
   18 /*
   19  *  Author: Isaac Traxler
   20  *    Date: 2014-10-29
   21  * Purpose: fun
   22  * Problem: 591 - Box of Bricks
   23  */
   24 
   25 /*
   26  * This template reads data a specified number of times.
   27  */
   28 
   29 int colCnt;
   30 int cols[MAX_COLS];
   31 int tot;
   32 
   33 void init()
   34 {
   35     /* FUNCTION init */
   36 } /* FUNCTION init */
   37 
   38 void dump()
   39 {
   40     /* FUNCTION dump */
   41 } /* FUNCTION dump */
   42 
   43 int getInput()
   44 {
   45     /* FUNCTION getInput */
   46     int i;
   47     int dataReadFlag = FALSE;
   48 
   49     scanf(" %d ", &colCnt);
   50     if (0 != colCnt)
   51         {
   52             /* more data to process */
   53             dataReadFlag = TRUE;
   54             tot = 0;
   55             for(i=0; i<colCnt; i++)
   56                 {
   57                     /* read each column */
   58                     scanf(" %d ", &cols[i]);
   59                     tot = tot + cols[i];
   60                 } /* read each column */
   61         } /* more data to process */
   62     return dataReadFlag;
   63 } /* FUNCTION getInput */
   64 
   65 void process()
   66 {
   67     /* FUNCTION process */
   68     int i;
   69     int avg;
   70 
   71     avg = tot / colCnt;
   72     tot = 0;
   73     for (i=0; i<colCnt; i++)
   74         {
   75             /* for each col */
   76             tot = tot + abs(cols[i] - avg);
   77         } /* for each col */
   78     tot = tot / 2;
   79     printf("The minimum number of moves is %d.\n\n", tot);
   80 
   81 } /* FUNCTION process */
   82 
   83 int main ()
   84 {
   85     /* main */
   86     int i = 0;
   87     int moreToDo;
   88 
   89     moreToDo = getInput();
   90     while (moreToDo)
   91         {
   92             /* while */
   93             i++;
   94             printf("Set #%d\n", i);
   95             process();
   96             moreToDo = getInput();
   97         } /* while */
   98 
   99     return EXIT_SUCCESS;
  100 } /* main */
  101 
  102