Computer Programming Contest Preparation

ToolBox - Source for: 120/12060/a.c



/home/toolbox/public_html/solutions/120/12060/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 <stdint.h>
    7 #include <math.h>
    8 #include <stdlib.h>
    9 
   10 #define TRUE  (1 == 1)
   11 #define FALSE (1 != 1)
   12 
   13 #define DEBUG if (FALSE)
   14 
   15 /*
   16  *  Author: Isaac Traxler
   17  *    Date: March 8, 2016
   18  * Purpose: fun
   19  * Problem: 12060
   20  */
   21 
   22 /*
   23  * This template reads data until a terminating value is reached.
   24  */
   25 
   26 int cnt;
   27 int sum;
   28 int numerator;
   29 int denominator;
   30 
   31 void init()
   32 {
   33     /* FUNCTION init */
   34 } /* FUNCTION init */
   35 
   36 void dump()
   37 {
   38     /* FUNCTION dump */
   39 } /* FUNCTION dump */
   40 
   41 int getInput()
   42 {
   43     /* FUNCTION getInput */
   44     int dataReadFlag;
   45     int i;
   46     int tmp;
   47 
   48     scanf(" %d ", &cnt);
   49     if (0 == cnt)
   50         {
   51             /* end of input reached */
   52             dataReadFlag = FALSE;
   53         } /* end of input reached */
   54     else
   55         {
   56             /* more read */
   57             dataReadFlag = TRUE;
   58             sum = 0;
   59             for (i=0; i<cnt; i++)
   60                 {
   61                     /* for eac value */
   62                     scanf(" %d ", &tmp);
   63                     sum = sum + tmp;
   64                 } /* for eac value */
   65         } /* more read */
   66     return (dataReadFlag);
   67 } /* FUNCTION getInput */
   68 
   69 void reduce()
   70 {
   71     /* FUNCTION reduce */
   72     int i;
   73 
   74     /* faster would be to fivide only by primes */
   75     for (i=2; i<=numerator; i++)
   76         {
   77             /* for each possible factor */
   78             while ((0 == (numerator % i)) && (0 == (denominator % i)))
   79                 {
   80                     /* common factor -- reduce */
   81                     numerator = numerator / i;
   82                     denominator = denominator / i;
   83                 } /* common factor -- reduce */
   84         } /* for each possible factor */
   85 } /* FUNCTION reduce */
   86 
   87 int digits(int num)
   88 {
   89     /* FUNCTION digits */
   90     int tmp;
   91     int i = 0;
   92 
   93     tmp = num;
   94     while (0 < tmp)
   95         {
   96             /* count digits */
   97             i++;
   98             tmp = tmp / 10;
   99         } /* count digits */
  100     return i;
  101 } /* FUNCTION digits */
  102 
  103 void process()
  104 {
  105     /* FUNCTION process */
  106     int negative;
  107     int whole;
  108     int i;
  109     int n;
  110     int d;
  111 
  112     negative = 0 > sum;
  113     if (negative)
  114         {
  115             /* negative number */
  116             sum = - sum;
  117         } /* negative number */
  118     whole = sum / cnt;
  119     numerator = sum % cnt;
  120     denominator = cnt;
  121     reduce();
  122 
  123     if (0 < numerator)
  124         {
  125             /* we need to print fraction */
  126             /* print line 1 */
  127             n = digits(numerator);
  128             d = digits(denominator);
  129             /* worry about sign */
  130             if (negative)
  131                 {
  132                     printf("  ");
  133                 }
  134             /* worry about whole part of division */
  135             if (0 < whole)
  136                 {
  137                     /* whole part present */
  138                     for (i=0; i<digits(whole); i++)
  139                         {
  140                             printf(" ");
  141                         }
  142                 } /* whole part present */
  143             /* worry about numerator */
  144             if (d>n)
  145                 {
  146                     /* denomiator has more digits than numerator */
  147                     for (i=0; i<(d-n); i++)
  148                         {
  149                             printf(" ");
  150                         }
  151                 } /* denomiator has more digits than numerator */
  152             printf("%d\n", numerator);
  153             /* print line 2 */
  154             /* worry about sign */
  155             if (negative)
  156                 {
  157                     printf("- ");
  158                 }
  159             if (0 < whole)
  160                 {
  161                     /* whole part present */
  162                     printf("%d", whole);
  163                 } /* whole part present */
  164             /* print dashes */
  165             for (i=0; i < d; i++)
  166                 {
  167                     printf("-");
  168                 }
  169             printf("\n");
  170             /* print line 3 */
  171             /* worry about sign */
  172             if (negative)
  173                 {
  174                     printf("  ");
  175                 }
  176             /* worry about whole part of division */
  177             if (0 < whole)
  178                 {
  179                     /* whole part present */
  180                     for (i=0; i<digits(whole); i++)
  181                         {
  182                             printf(" ");
  183                         }
  184                 } /* whole part present */
  185             printf("%d\n", denominator);
  186         } /* we need to print fraction */
  187     else
  188         {
  189             /* no fraction */
  190             if (negative)
  191                 {
  192                     printf("- ");
  193                 }
  194             printf("%d\n", whole);
  195         } /* no fraction */
  196 
  197 } /* FUNCTION process */
  198 
  199 int main()
  200 {
  201     /* main */
  202     int moreToDo;
  203     int i = 1;
  204 
  205     init();
  206     moreToDo = getInput();
  207     while (moreToDo)
  208         {
  209             /* while */
  210             printf("Case %d:\n", i++);
  211             process();
  212             moreToDo = getInput();
  213         } /* while */
  214 
  215     return EXIT_SUCCESS;
  216 } /* main */
  217