Computer Programming Contest Preparation

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



/home/toolbox/public_html/solutions/101/10137/b.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 over1 =0;
   37     int fudge;
   38     int numOvers = 0;
   39     int magic;
   40     int avg1;
   41 
   42     avg = total / numStudents;
   43     leftover = total - (avg * numStudents);
   44     avg1 = (numStudents > (2 * leftover)) ? avg : avg + 1;
   45     for (i =0; i< numStudents; i++)
   46         {
   47             printf("%d ",money[i]);
   48             if (money[i] < avg)
   49                 {
   50                     under += avg - money[i];
   51                 }
   52             else if (money[i] > avg)
   53                 {
   54                     numOvers++;
   55                     over += money[i] - avg;
   56                     over1 += money[i] - avg1;
   57                 }
   58         }
   59     magic =  (avg != avg1) ? over1 : under;
   60     fudge = (leftover > numOvers) ? over - numOvers : over - leftover;
   61     printf("\n under [%d] ", under);
   62     printf(" over [%d]", over);
   63     printf(" fudge [%d] ", fudge);
   64     printf(" magic [%d] \n", magic);
   65 
   66 } // END FUNCTION process
   67 
   68 int main ()
   69 {
   70     // main
   71     int moreToDo;
   72     long int money[1000];
   73     long int total;
   74     int numStudents;
   75 
   76     //init();
   77     moreToDo = getInput(&numStudents, money, &total);
   78     while (moreToDo)
   79         {
   80             // while
   81             printf("\n numStudents [%d] ", numStudents);
   82             printf(" total [%ld]\n", total);
   83             process(total, numStudents, money);
   84             moreToDo = getInput(&numStudents, money, &total);
   85         } // while
   86 
   87     return 1;
   88 } // main
   89