Computer Programming Contest Preparation

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



/home/toolbox/public_html/solutions/7/725/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: 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 num;
   28 int den;
   29 int goal;
   30 
   31 void process()
   32 {
   33     /* FUNCTION init */
   34     int i;
   35     int i0, i1, i2, i3, i4, i5, i6, i7, i8, i9;
   36     int res;
   37     int cnt = 0;
   38 
   39     /* generate all denominators and see if any goal numerators exist */
   40     for (i5=0; 9>i5; i5++) /* first digit of denominator */
   41         {
   42             /* for i5 */
   43             for (i6=0; 10>i6; i6++)
   44                 {
   45                     /* for i6 */
   46                     if (i5 != i6)
   47                         {
   48                             /* valid case */
   49                             for (i7=0; 10>i7; i7++)
   50                                 {
   51                                     /* for i7 */
   52                                     if ((i5 != i7) && (i6 != i7))
   53                                         {
   54                                             /* valid case */
   55                                             for (i8=0; 10>i8; i8++)
   56                                                 {
   57                                                     /* for i8 */
   58                                                     if ((i5 != i8) && (i6 != i8) && (i7 != i8))
   59                                                         {
   60                                                             /* valid case */
   61                                                             for (i9=0; 10>i9; i9++)
   62                                                                 {
   63                                                                     /* for i9 */
   64                                                                     if ((i5 != i9) && (i6 != i9) && (i7 != i9) && (i8 != i9))
   65                                                                         {
   66                                                                             /* valid case */
   67                                                                             den = 10000 * i5 + 1000 * i6 + 100 * i7 + 10 * i8 + i9;
   68                                                                             res = goal * den;
   69                                                                             if (100000 > res)
   70                                                                                 {
   71                                                                                     /* numerator is in range */
   72                                                                                     i4 = res % 10;
   73                                                                                     if ((i4 != i5) && (i4 != i6) && (i4 != i7) && (i4 != i8) && (i4 != i9))
   74                                                                                         {
   75                                                                                             /* valid digit */
   76                                                                                             res = res / 10;
   77                                                                                             i3 = res % 10;
   78                                                                                             if ((i3 != i4) && (i3 != i5) && (i3 != i6) && (i3 != i7) && (i3 != i8) && (i3 != i9))
   79                                                                                                 {
   80                                                                                                     /* valid digit */
   81                                                                                                     res = res / 10;
   82                                                                                                     i2 = res % 10;
   83                                                                                                     if ((i2 != i3) && (i2 != i4) && (i2 != i5) && (i2 != i6) && (i2 != i7) && (i2 != i8) && (i2 != i9))
   84                                                                                                         {
   85                                                                                                             /* valid digit */
   86                                                                                                             res = res / 10;
   87                                                                                                             i1 = res % 10;
   88                                                                                                             if ((i1 != i2) && (i1 != i3) && (i1 != i4) && (i1 != i5) && (i1 != i6) && (i1 != i7) && (i1 != i8) && (i1 != i9))
   89                                                                                                                 {
   90                                                                                                                     /* valid digit */
   91                                                                                                                     i0 = res / 10;
   92                                                                                                                     if ((i0 != i1) && (i0 != i2) && (i0 != i3) && (i0 != i4) && (i0 != i5) && (i0 != i6) && (i0 != i7) && (i0 != i8) && (i0 != i9))
   93                                                                                                                         {
   94                                                                                                                             /* valid digit */
   95                                                                                                                             printf("%d%d%d%d%d / %05d = %d\n", i0, i1, i2, i3, i4, den, goal);
   96                                                                                                                             cnt ++;
   97                                                                                                                         } /* valid digit */
   98                                                                                                                 } /* valid digit */
   99                                                                                                         } /* valid digit */
  100                                                                                                 } /* valid digit */
  101                                                                                         } /* valid digit */
  102                                                                                 } /* numerator is in range */
  103                                                                         } /* valid case */
  104                                                                 } /* for i9 */
  105                                                         } /* valid case */
  106                                                 } /* for i8 */
  107                                         } /* valid case */
  108                                 } /* for i7 */
  109                         } /* valid case */
  110                 } /* for i6 */
  111         } /* for i5 */
  112     if (0 == cnt)
  113         {
  114             /* no matches */
  115             printf("There are no solutions for %d.\n", goal);
  116         } /* no matches */
  117 } /* FUNCTION init */
  118 
  119 void dump()
  120 {
  121     /* FUNCTION dump */
  122 } /* FUNCTION dump */
  123 
  124 int getInput()
  125 {
  126     /* FUNCTION getInput */
  127     int dataReadFlag;
  128 
  129     scanf(" %d ", &goal);
  130 
  131     dataReadFlag = 0 != goal;
  132     return (dataReadFlag);
  133 } /* FUNCTION getInput */
  134 
  135 int main()
  136 {
  137     /* main */
  138     int moreToDo;
  139     int first = TRUE;
  140 
  141     moreToDo = getInput();
  142     while (moreToDo)
  143         {
  144             /* while */
  145             if (! first)
  146                 {
  147                     printf("\n");
  148                 }
  149             first = FALSE;
  150             process();
  151             moreToDo = getInput();
  152         } /* while */
  153 
  154     return EXIT_SUCCESS;
  155 } /* main */
  156