Computer Programming Contest Preparation

ToolBox - Source for: 7/725/c.c



/home/toolbox/public_html/solutions/7/725/c.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: 2015-04-03
   18  * Purpose:
   19  * Problem: 725
   20  */
   21 
   22 /*
   23  * This template reads data until a terminating value is reached.
   24  */
   25 
   26 #define UPPER_LIMIT 80
   27 int ansCnt[UPPER_LIMIT];
   28 long long int ans[UPPER_LIMIT][250];
   29 /* first column will be count of answers, each successive column will be an answer */
   30 int num;
   31 int den;
   32 int goal;
   33 
   34 void init()
   35 {
   36     /* FUNCTION init */
   37     int i;
   38     int i0, i1, i2, i3, i4, i5, i6, i7, i8, i9;
   39     int res;
   40 
   41     for (i=2; UPPER_LIMIT>i; i++)
   42         {
   43             /* for */
   44             ansCnt[i] = 0;
   45         } /* for */
   46     /* process through ever possible numerator,
   47        then test each possible denominator for that numerator */
   48     for (i0=1; 10>i0; i0++)
   49         {
   50             /* for i0 */
   51             for (i1=0; 10>i1; i1++)
   52                 {
   53                     /* for i1 */
   54                     if (i0 != i1)
   55                         {
   56                             /* valid case */
   57                             for (i2=0; 10>i2; i2++)
   58                                 {
   59                                     /* for i2 */
   60                                     if ((i0 != i2) && (i1 != i2))
   61                                         {
   62                                             /* valid case */
   63                                             for (i3=0; 10>i3; i3++)
   64                                                 {
   65                                                     /* for i3 */
   66                                                     if ((i0 != i3) && (i1 != i3) && (i2 != i3))
   67                                                         {
   68                                                             /* valid case */
   69                                                             for (i4=0; 10>i4; i4++)
   70                                                                 {
   71                                                                     /* for i4 */
   72                                                                     if ((i0 != i4) && (i1 != i4) && (i2 != i4) && (i3 != i4))
   73                                                                         {
   74                                                                             /* valid case */
   75                                                                             for (i5=1; 9>i5; i5++) /* first digit of denominator */
   76                                                                                 {
   77                                                                                     /* for i5 */
   78                                                                                     if ((i0 != i5) && (i1 != i5) && (i2 != i5) && (i3 != i5) && (i4 != i5))
   79                                                                                         {
   80                                                                                             /* valid case */
   81                                                                                             for (i6=0; 10>i6; i6++)
   82                                                                                                 {
   83                                                                                                     /* for i6 */
   84                                                                                                     if ((i0 != i6) && (i1 != i6) && (i2 != i6) && (i3 != i6) && (i4 != i6) && (i5 != i6))
   85                                                                                                         {
   86                                                                                                             /* valid case */
   87                                                                                                             for (i8=0; 10>i8; i8++)
   88                                                                                                                 {
   89                                                                                                                     /* for i8 */
   90                                                                                                                     if ((i0 != i8) && (i1 != i8) && (i2 != i8) && (i3 != i8) && (i4 != i8) && (i5 != i8) && (i6 != i8) && (i7 != i8))
   91                                                                                                                         {
   92                                                                                                                             /* valid case */
   93                                                                                                                             for (i8=0; 10>i8; i8++)
   94                                                                                                                                 {
   95                                                                                                                                     /* for i8 */
   96                                                                                                                                     if ((i0 != i8) && (i1 != i8) && (i2 != i8) && (i3 != i8) && (i4 != i8) && (i5 != i8) && (i6 != i8) && (i7 != i8))
   97                                                                                                                                         {
   98                                                                                                                                             /* valid case */
   99                                                                                                                                             for (i9=0; 10>i9; i9++)
  100                                                                                                                                                 {
  101                                                                                                                                                     /* for i9 */
  102                                                                                                                                                     if ((i0 != i9) && (i1 != i9) && (i2 != i9) && (i3 != i9) && (i4 != i9) && (i5 != i9) && (i6 != i9) && (i7 != i9) && (i8 != i9))
  103                                                                                                                                                         {
  104                                                                                                                                                             /* valid case */
  105                                                                                                                                                             num = i0 + 10 * i1 + 100 * i2 + 1000 * i3 + 10000 * i4;
  106                                                                                                                                                             den = i5 + 10 * i6 + 100 * i7 + 1000 * i8 + 10000 * i9;
  107                                                                                                                                                             if (num > den)
  108                                                                                                                                                                 {
  109                                                                                                                                                                     /* do actual division */
  110                                                                                                                                                                     res = num / den;
  111                                                                                                                                                                     if (1 < res)
  112                                                                                                                                                                         {
  113                                                                                                                                                                             /* ignore answeres below 2 */
  114                                                                                                                                                                             printf("%d / %d = %d\n", num, den, res);
  115                                                                                                                                                                             ans[res][ansCnt[res]] = ((long long int) num) * 100000 / den;
  116                                                                                                                                                                             ansCnt[res] = ansCnt[res] + 1;
  117                                                                                                                                                                         } /* ignore answeres below 2 */
  118                                                                                                                                                                 } /* do actual division */
  119                                                                                                                                                         } /* valid case */
  120                                                                                                                                                 } /* for i9 */
  121                                                                                                                                         } /* valid case */
  122                                                                                                                                 } /* for i8 */
  123                                                                                                                         } /* valid case */
  124                                                                                                                 } /* for i7 */
  125                                                                                                         } /* valid case */
  126                                                                                                 } /* for i6 */
  127                                                                                         } /* valid case */
  128                                                                                 } /* for i5 */
  129                                                                         } /* valid case */
  130                                                                 } /* for i4 */
  131                                                         } /* valid case */
  132                                                 } /* for i3 */
  133                                         } /* valid case */
  134                                 } /* for i2 */
  135                         } /* valid case */
  136                 } /* for i1 */
  137         } /* for i0 */
  138 
  139 } /* FUNCTION init */
  140 
  141 void dump()
  142 {
  143     /* FUNCTION dump */
  144 } /* FUNCTION dump */
  145 
  146 int getInput()
  147 {
  148     /* FUNCTION getInput */
  149     int dataReadFlag;
  150 
  151     scanf(" %d ", &goal);
  152 
  153     dataReadFlag = 0 != goal;
  154     return (dataReadFlag);
  155 } /* FUNCTION getInput */
  156 
  157 void process()
  158 {
  159     /* FUNCTION process */
  160     int i;
  161     for (i=2; UPPER_LIMIT>i; i++)
  162         {
  163             /* for */
  164             printf("ansCnt[%d] = %d\n", i, ansCnt[i]);
  165         } /* for */
  166 } /* FUNCTION process */
  167 
  168 int main()
  169 {
  170     /* main */
  171     int moreToDo;
  172 
  173     init();
  174     moreToDo = getInput();
  175     while (moreToDo)
  176         {
  177             /* while */
  178             process();
  179             moreToDo = getInput();
  180         } /* while */
  181 
  182     return EXIT_SUCCESS;
  183 } /* main */
  184