Computer Programming Contest Preparation

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



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