Computer Programming Contest Preparation

ToolBox - Source for: 9/993/a.c



/home/toolbox/public_html/solutions/9/993/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 <stdlib.h>
    7 #include <math.h>
    8 #include <stdint.h>
    9 #include <ctype.h>
   10 
   11 #define TRUE  (1 == 1)
   12 #define FALSE (1 != 1)
   13 
   14 #define DEBUG if (FALSE)
   15 
   16 /* fprintf(stderr, "functionName: message", varslist); */
   17 
   18 /*
   19  *  Author: Isaac Traxler
   20  *    Date: 2021-12-03
   21  * Purpose: fun
   22  * Problem: 993 - Product of digits
   23  */
   24 
   25 /*
   26  * This template reads data a specified number of times.
   27  */
   28 
   29 #define MAX_LINE 256
   30 #define TWO 0
   31 #define THREE 1
   32 #define FIVE 2
   33 #define SEVEN 3
   34 
   35 int numberOfTimes;
   36 int number;
   37 int divisors[4] = {2, 3, 5, 7};
   38 int counts[4];
   39 char line[MAX_LINE];
   40 int lineCnt;
   41 
   42 void init()
   43 {
   44     /* FUNCTION init */
   45     scanf("%d ", &numberOfTimes);
   46 } /* FUNCTION init */
   47 
   48 void dump()
   49 {
   50     /* FUNCTION dump */
   51 } /* FUNCTION dump */
   52 
   53 void getInput()
   54 {
   55     /* FUNCTION getInput */
   56     scanf(" %d ", &number);
   57     counts[0] = 0;
   58     counts[1] = 0;
   59     counts[2] = 0;
   60     counts[3] = 0;
   61 } /* FUNCTION getInput */
   62 
   63 int facter(int x)
   64 {
   65     /* FUNCTION facter */
   66     int i;
   67     int tmp;
   68 
   69     tmp = x;
   70     for (i=0; (1 < tmp) && (4 > i); )
   71         {
   72             /* for each prime factor */
   73             if (0 == (tmp % divisors[i]))
   74                 {
   75                     /* found a factor */
   76                     counts[i]++;
   77                     tmp = tmp / divisors[i];
   78                 } /* found a factor */
   79             else
   80                 {
   81                     /* move on to next factor */
   82                     i++;
   83                 } /* move on to next factor */
   84         } /* for each prime factor */
   85     return (1 == tmp);
   86 } /* FUNCTION facter */
   87 
   88 void doit(int idx, int min, char digit)
   89 {
   90     /* FUNCTION doit */
   91     while (min <= counts[idx])
   92         {
   93             /* while */
   94             line[lineCnt] = digit;
   95             lineCnt++;
   96             counts[idx] = counts[idx] - min;
   97         } /* while */
   98 } /* FUNCTION doit */
   99 
  100 void doSix()
  101 {
  102     /* FUNCTION doSix */
  103     while ((1 <= counts[TWO]) && (1 <= counts[THREE]))
  104         {
  105             /* while */
  106             line[lineCnt] = '6';
  107             lineCnt++;
  108             counts[THREE] = counts[THREE] - 1;
  109             counts[TWO] = counts[TWO] - 1;
  110         } /* while */
  111 } /* FUNCTION doSix */
  112 
  113 void process()
  114 {
  115     /* FUNCTION process */
  116     int i;
  117 
  118     if (0 == number)
  119         {
  120             /* trivial case */
  121             printf("0\n");
  122         } /* trivial case */
  123     else if (1 == number)
  124         {
  125             /* trivial case */
  126             printf("1\n");
  127         } /* trivial case */
  128     else
  129         {
  130             /* do the math */
  131             if (facter(number))
  132                 {
  133                     /* can be done */
  134                     DEBUG printf("%d %d %d %d\n", counts[TWO], counts[THREE], counts[FIVE], counts[SEVEN]);
  135                     lineCnt = 0;
  136                     doit(THREE, 2, '9');
  137                     doit(TWO, 3, '8');
  138                     doit(SEVEN, 1, '7');
  139                     doSix();
  140                     doit(FIVE, 1, '5');
  141                     doit(TWO, 2, '4');
  142                     doit(THREE, 1, '3');
  143                     doit(TWO, 1, '2');
  144                     for (i=lineCnt-1; 0<=i; i--)
  145                         {
  146                             /* print each digit */
  147                             printf("%c", line[i]);
  148                         } /* print each digit */
  149                     printf("\n");
  150                 } /* can be done */
  151             else
  152                 {
  153                     /* can not be done */
  154                     printf("-1\n");
  155                 } /* can not be done */
  156         } /* do the math */
  157 
  158 } /* FUNCTION process */
  159 
  160 int main()
  161 {
  162     /* main */
  163     int i;
  164 
  165     init();
  166     for (i=0; i<numberOfTimes; i++)
  167         {
  168             /* while */
  169             getInput();
  170             process();
  171         } /* while */
  172 
  173     return EXIT_SUCCESS;
  174 } /* main */
  175 
  176