Computer Programming Contest Preparation

ToolBox - Source for: 109/10976/a.c



/home/toolbox/public_html/solutions/109/10976/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 #define MAX_LINE 257
   16 
   17 /*
   18  *  Author: Isaac Traxler
   19  *    Date: 2020-03-31
   20  * Purpose: fun
   21  * Problem: 10976 - Fraction again?
   22  */
   23 
   24 /*
   25  * This template reads lines of data at a time until end of file.
   26  */
   27 
   28 int numDen;
   29 int upDen;
   30 int lowDen;
   31 
   32 void init()
   33 {
   34     /* FUNCTION init */
   35 } /* FUNCTION init */
   36 
   37 void dump()
   38 {
   39     /* FUNCTION dump */
   40 } /* FUNCTION dump */
   41 
   42 int getInput()
   43 {
   44     /* FUNCTION getInput */
   45     int dataReadFlag;
   46 
   47     dataReadFlag = (1 == scanf(" %d ", &numDen));
   48     return (dataReadFlag);
   49 } /* FUNCTION getInput */
   50 
   51 /*
   52  * 1/2 = 1/? + 1/3
   53  * 1/2 - 1/3 = 1/?
   54  * 3/6 - 2/6 = 1/?
   55  */
   56 
   57 int check()
   58 {
   59     /* FUNCTION check */
   60     int commonDen;
   61     int upNum;
   62 
   63     commonDen = lowDen * numDen;
   64     upDen = commonDen;
   65     upNum = lowDen - numDen;
   66     DEBUG printf("(commonDen %d) (upNum %d) (lowDen %d} (upDen %d)\n", commonDen, upNum, lowDen, upDen);
   67     if (1 != upNum)
   68         {
   69             /* can it be reduced */
   70             if (0 == (commonDen % upNum))
   71                 {
   72                     /* can be reduced */
   73                     upDen = commonDen / upNum;
   74                     upNum = 1;
   75                 } /* can be reduced */
   76         } /* can it be reduced */
   77     return (1 == upNum);
   78 } /* FUNCTION check */
   79 
   80 void process()
   81 {
   82     /* FUNCTION process */
   83     int keepGoing = TRUE;
   84     int cnt = 0;
   85     int keep;
   86 
   87     keep = numDen;
   88     DEBUG printf("%d\n", numDen);
   89     lowDen = numDen + 1;
   90     while (keepGoing)
   91         {
   92             /* while */
   93             if (check())
   94                 {
   95                     /* work */
   96                     cnt++;
   97                 } /* work */
   98             DEBUG printf("(lowDen %d) (upDen %d)\n", lowDen, upDen);
   99             keepGoing = lowDen < upDen;
  100             lowDen++;
  101         } /* while */
  102 
  103     printf("%d\n", cnt);
  104     numDen = keep;
  105     lowDen = numDen + 1;
  106     keepGoing = TRUE;
  107     while (keepGoing)
  108         {
  109             /* while */
  110             if (check())
  111                 {
  112                     /* work */
  113                     printf("1/%d = 1/%d + 1/%d\n", numDen, upDen, lowDen);
  114                 } /* work */
  115             DEBUG printf("(lowDen %d) (upDen %d)\n", lowDen, upDen);
  116             keepGoing = lowDen < upDen;
  117             lowDen++;
  118         } /* while */
  119 
  120 } /* FUNCTION process */
  121 
  122 int main()
  123 {
  124     /* main */
  125     int moreToDo;
  126 
  127     init();
  128     moreToDo = getInput();
  129     while (moreToDo)
  130         {
  131             /* while */
  132             process();
  133             moreToDo = getInput();
  134         } /* while */
  135 
  136     return EXIT_SUCCESS;
  137 } /* main */
  138