Computer Programming Contest Preparation

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



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