Computer Programming Contest Preparation

ToolBox - Source for: 101/10137/c.c



/home/toolbox/public_html/solutions/101/10137/c.c
    1 #include <stdio.h>
    2 
    3 #define TRUE  (1 == 1)
    4 #define FALSE (1 != 1)
    5 #define DEBUG if (FALSE)
    6 
    7 int getInput(int * numStudents, long int money[], long int *total)
    8 {
    9     /* BEGIN FUNCTION getInput */
   10     int i;
   11     double m;
   12     int rc = 0;
   13 
   14     scanf("%d ", numStudents);
   15     if (0 < *numStudents)
   16         {
   17             *total = 0;
   18             rc = 1;
   19             for (i = 0; i < *numStudents; i++)
   20                 {
   21                     scanf("%lf ", &m);
   22                     money[i] = (long int)((m * 100) + 0.5);
   23                     *total += money[i];
   24                 }
   25         }
   26     return(rc);
   27 } /* END FUNCTION getInput */
   28 
   29 void process(long int total, int numStudents, long int money[])
   30 {
   31     /* BEGIN FUNCTION process */
   32     long int avg;
   33     int remainder;
   34     int i;
   35     int changeHands = 0;
   36 
   37     avg = total / numStudents;
   38     remainder = total % numStudents;
   39 
   40     for (i=0; i<numStudents; i++)
   41         {
   42             /* for */
   43             if (avg < money[i])
   44                 {
   45                     /* take some money */
   46                     if (0 < remainder)
   47                         {
   48                             /* remainder left */
   49                             changeHands = changeHands + money[i] - (avg + 1);
   50                             remainder --;
   51                         } /* remainder left */
   52                     else
   53                         {
   54                             /* no remainder left */
   55                             changeHands = changeHands + money[i] - avg;
   56                         } /* no remainder left */
   57                 } /* take some money */
   58         } /* for */
   59     printf("$%d.%02d\n", changeHands/100, changeHands%100);
   60 } /* END FUNCTION process */
   61 
   62 int main ()
   63 {
   64     /* main */
   65     int moreToDo;
   66     long int money[1000];
   67     long int total;
   68     int numStudents;
   69 
   70     /*init(); */
   71     moreToDo = getInput(&numStudents, money, &total);
   72     while (moreToDo)
   73         {
   74             /* while */
   75             DEBUG printf("\n numStudents [%d] ", numStudents);
   76             DEBUG printf(" total [%ld]\n", total);
   77             process(total, numStudents, money);
   78             moreToDo = getInput(&numStudents, money, &total);
   79         } /* while */
   80 
   81     return 1;
   82 } /* main */
   83