Computer Programming Contest Preparation

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



/home/toolbox/public_html/solutions/7/725/slow.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     /* process through ever possible numerator,
   40        then test each possible denominator for that numerator */
   41     for (i0=1; 10>i0; i0++) /* first digit of numerator */
   42         {
   43             /* for i0 */
   44             for (i1=0; 10>i1; i1++)
   45                 {
   46                     /* for i1 */
   47                     if (i0 != i1)
   48                         {
   49                             /* valid case */
   50                             for (i2=0; 10>i2; i2++)
   51                                 {
   52                                     /* for i2 */
   53                                     if ((i0 != i2) && (i1 != i2))
   54                                         {
   55                                             /* valid case */
   56                                             for (i3=0; 10>i3; i3++)
   57                                                 {
   58                                                     /* for i3 */
   59                                                     if ((i0 != i3) && (i1 != i3) && (i2 != i3))
   60                                                         {
   61                                                             /* valid case */
   62                                                             for (i4=0; 10>i4; i4++)
   63                                                                 {
   64                                                                     /* for i4 */
   65                                                                     if ((i0 != i4) && (i1 != i4) && (i2 != i4) && (i3 != i4))
   66                                                                         {
   67                                                                             /* valid case */
   68                                                                             for (i5=0; 9>i5; i5++) /* first digit of denominator */
   69                                                                                 {
   70                                                                                     /* for i5 */
   71                                                                                     if ((i0 != i5) && (i1 != i5) && (i2 != i5) && (i3 != i5) && (i4 != i5))
   72                                                                                         {
   73                                                                                             /* valid case */
   74                                                                                             for (i6=0; 10>i6; i6++)
   75                                                                                                 {
   76                                                                                                     /* for i6 */
   77                                                                                                     if ((i0 != i6) && (i1 != i6) && (i2 != i6) && (i3 != i6) && (i4 != i6) && (i5 != i6))
   78                                                                                                         {
   79                                                                                                             /* valid case */
   80                                                                                                             for (i7=0; 10>i7; i7++)
   81                                                                                                                 {
   82                                                                                                                     /* for i7 */
   83                                                                                                                     if ((i0 != i7) && (i1 != i7) && (i2 != i7) && (i3 != i7) && (i4 != i7) && (i5 != i7) && (i6 != i7))
   84                                                                                                                         {
   85                                                                                                                             /* valid case */
   86                                                                                                                             for (i8=0; 10>i8; i8++)
   87                                                                                                                                 {
   88                                                                                                                                     /* for i8 */
   89                                                                                                                                     if ((i0 != i8) && (i1 != i8) && (i2 != i8) && (i3 != i8) && (i4 != i8) && (i5 != i8) && (i6 != i8) && (i7 != i8))
   90                                                                                                                                         {
   91                                                                                                                                             /* valid case */
   92                                                                                                                                             for (i9=0; 10>i9; i9++)
   93                                                                                                                                                 {
   94                                                                                                                                                     /* for i9 */
   95                                                                                                                                                     if ((i0 != i9) && (i1 != i9) && (i2 != i9) && (i3 != i9) && (i4 != i9) && (i5 != i9) && (i6 != i9) && (i7 != i9) && (i8 != i9))
   96                                                                                                                                                         {
   97                                                                                                                                                             /* valid case */
   98                                                                                                                                                             num = 10000 * i0 + 1000 * i1 + 100 * i2 + 10 * i3 + i4;
   99                                                                                                                                                             den = 10000 * i5 + 1000 * i6 + 100 * i7 + 10 * i8 + i9;
  100                                                                                                                                                             if (num > den)
  101                                                                                                                                                                 {
  102                                                                                                                                                                     /* do actual division */
  103                                                                                                                                                                     if (0 == (num % den))
  104                                                                                                                                                                         {
  105                                                                                                                                                                             /* must divide actually */
  106                                                                                                                                                                             res = num / den;
  107                                                                                                                                                                             if (goal == res)
  108                                                                                                                                                                                 {
  109                                                                                                                                                                                     /* match */
  110                                                                                                                                                                                     cnt++;
  111                                                                                                                                                                                     printf("%d / %05d = %d\n", num, den, res);
  112                                                                                                                                                                                 } /* match */
  113                                                                                                                                                                         } /* must divide actually */
  114                                                                                                                                                                 } /* do actual division */
  115                                                                                                                                                         } /* valid case */
  116                                                                                                                                                 } /* for i9 */
  117                                                                                                                                         } /* valid case */
  118                                                                                                                                 } /* for i8 */
  119                                                                                                                         } /* valid case */
  120                                                                                                                 } /* for i7 */
  121                                                                                                         } /* valid case */
  122                                                                                                 } /* for i6 */
  123                                                                                         } /* valid case */
  124                                                                                 } /* for i5 */
  125                                                                         } /* valid case */
  126                                                                 } /* for i4 */
  127                                                         } /* valid case */
  128                                                 } /* for i3 */
  129                                         } /* valid case */
  130                                 } /* for i2 */
  131                         } /* valid case */
  132                 } /* for i1 */
  133         } /* for i0 */
  134     if (0 == cnt)
  135         {
  136             /* no matches */
  137             printf("There are no solutions for %d.\n", goal);
  138         } /* no matches */
  139     printf("\n");
  140 } /* FUNCTION init */
  141 
  142 void dump()
  143 {
  144     /* FUNCTION dump */
  145 } /* FUNCTION dump */
  146 
  147 int getInput()
  148 {
  149     /* FUNCTION getInput */
  150     int dataReadFlag;
  151 
  152     scanf(" %d ", &goal);
  153 
  154     dataReadFlag = 0 != goal;
  155     return (dataReadFlag);
  156 } /* FUNCTION getInput */
  157 
  158 int main()
  159 {
  160     /* main */
  161     int moreToDo;
  162 
  163     moreToDo = getInput();
  164     while (moreToDo)
  165         {
  166             /* while */
  167             process();
  168             moreToDo = getInput();
  169         } /* while */
  170 
  171     return EXIT_SUCCESS;
  172 } /* main */
  173