Computer Programming Contest Preparation

ToolBox - Source for: 101/10190/a.c



/home/toolbox/public_html/solutions/101/10190/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: 2021-01-14
   20  * Purpose: fun
   21  * Problem: 10190 - Divide, But Not Quite Conquer!
   22  */
   23 
   24 /*
   25  * This template reads lines of data at a time until end of file.
   26  */
   27 
   28 int n;       /* input - number to start with (name taken from problem statement) */
   29 int m;       /* input -number to divide by (name taken from problem statement) */
   30 int buf[40]; /* cache, used to hold division results */
   31 int cnt;     /* count of how many results were saved - note that 2147483648 2, will produce
   32                 23 results, so 40 is overkill */
   33 
   34 void init()
   35 {
   36     /* FUNCTION init */
   37 } /* FUNCTION init */
   38 
   39 void dump()
   40 {
   41     /* FUNCTION dump */
   42 } /* FUNCTION dump */
   43 
   44 int getInput()
   45 {
   46     /* FUNCTION getInput */
   47     int dataReadFlag;
   48 
   49     dataReadFlag = (2 == scanf(" %d %d ", &n, &m));
   50     return (dataReadFlag);
   51 } /* FUNCTION getInput */
   52 
   53 void process()
   54 {
   55     /* FUNCTION process */
   56     int tmp;
   57     int valid;
   58     int i;
   59 
   60     tmp = n;
   61     /* eliminate invalid cases */
   62     if ((n < m) || (2 > n) || (2 > m))
   63         {
   64             /* bad case */
   65             printf("Boring!\n");
   66         } /* bad case */
   67     else
   68         {
   69             /* possibly valid cases */
   70             cnt = 0;
   71             valid = 0 == (tmp % m); /* remain valid as long as division has no remainder */
   72             while ((valid) && (tmp > m))
   73                 {
   74                     /* while */
   75                     tmp = tmp / m;
   76                     buf[cnt] = tmp; /* cache result */
   77                     cnt++;
   78                     valid = 0 == (tmp % m); /* remain valid as long as division has no remainder */
   79                 } /* while */
   80             if (valid)
   81                 {
   82                     /* valid */
   83                     printf("%d ", n); /* dump N, because it is not put in cache */
   84                     for (i=0; cnt>i; i++)
   85                         {
   86                             /* for */
   87                             printf("%d ", buf[i]);
   88                         } /* for */
   89                     printf("1\n"); /* must dump the one since while loops stops short of last
   90                            division so that there will not be a trailing space */
   91                 } /* valid */
   92             else
   93                 {
   94                     /* invalid */
   95                     printf("Boring!\n");
   96                 } /* invalid */
   97         } /* possibly valid cases */
   98 } /* FUNCTION process */
   99 
  100 int main()
  101 {
  102     /* main */
  103     int moreToDo;
  104 
  105     init();
  106     moreToDo = getInput();
  107     while (moreToDo)
  108         {
  109             /* while */
  110             process();
  111             moreToDo = getInput();
  112         } /* while */
  113     return EXIT_SUCCESS;
  114 } /* main */
  115