Computer Programming Contest Preparation

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



/home/toolbox/public_html/solutions/109/10976/b.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 
   85     printf("%d\n", numDen);
   86     lowDen = numDen + 1;
   87     while (keepGoing)
   88         {
   89             /* while */
   90             if (check())
   91                 {
   92                     /* work */
   93                     printf("1/%d = 1/%d + 1/%d\n", numDen, upDen, lowDen);
   94                 } /* work */
   95             DEBUG printf("(lowDen %d) (upDen %d)\n", lowDen, upDen);
   96             keepGoing = lowDen < upDen;
   97             lowDen++;
   98         } /* while */
   99 
  100 
  101 } /* FUNCTION process */
  102 
  103 int main()
  104 {
  105     /* main */
  106     int moreToDo;
  107 
  108     init();
  109     moreToDo = getInput();
  110     while (moreToDo)
  111         {
  112             /* while */
  113             process();
  114             moreToDo = getInput();
  115         } /* while */
  116 
  117     return EXIT_SUCCESS;
  118 } /* main */
  119