Computer Programming Contest Preparation

ToolBox - Source for: 5/568/a.c



/home/toolbox/public_html/solutions/5/568/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 #include <ctype.h>
   10 
   11 #define TRUE  (1 == 1)
   12 #define FALSE (1 != 1)
   13 
   14 #define DEBUG if (FALSE)
   15 
   16 /*
   17  *  Author: Isaac Traxler
   18  *    Date: 2023-10-17
   19  * Purpose: fun
   20  * Problem: 568
   21  */
   22 
   23 /*
   24  * This template reads data until a terminating value is reached.
   25  */
   26 
   27 #define MAXX 10003
   28 
   29 int fac[MAXX];
   30 int idx;
   31 
   32 void init()
   33 {
   34     /* FUNCTION init */
   35     int i;
   36     long f;
   37     /* f must be a long, because it must be big enough to handle 10000 * running
   38      * total which must be big enough to 9offset all the 5s */
   39 
   40     f = 1;
   41     fac[0] = 1;
   42     for (i=1; MAXX>i; i++)
   43         {
   44             /* for each possible input */
   45             f = f * i;
   46             /* remove trailing zeroes */
   47             while (0 == (f % 10))
   48                 {
   49                     f = f / 10;
   50                 }
   51             /* keep f relatively small - f must be big enough to cancel all 5s in i out */
   52             /*   147483647 */
   53             f = f % 1000000;
   54             /* save current last digit */
   55             fac[i] = f % 10;
   56             DEBUG printf("fac[%d] = %d  (f %ld)\n", i, fac[i], f);
   57         } /* for each possible input */
   58 
   59 } /* FUNCTION init */
   60 
   61 void dump()
   62 {
   63     /* FUNCTION dump */
   64 } /* FUNCTION dump */
   65 
   66 int getInput()
   67 {
   68     /* FUNCTION getInput */
   69     int dataReadFlag;
   70 
   71     dataReadFlag = ! feof(stdin);
   72     if (dataReadFlag)
   73         {
   74             /* something to read */
   75             scanf(" %d ", &idx);
   76         } /* something to read */
   77     return (dataReadFlag);
   78 } /* FUNCTION getInput */
   79 
   80 void process()
   81 {
   82     /* FUNCTION process */
   83     printf("%5d -> %d\n", idx, fac[idx]);
   84 } /* FUNCTION process */
   85 
   86 int main()
   87 {
   88     /* main */
   89     int moreToDo;
   90 
   91     init();
   92     moreToDo = getInput();
   93     while (moreToDo)
   94         {
   95             /* while */
   96             process();
   97             moreToDo = getInput();
   98         } /* while */
   99 
  100     return EXIT_SUCCESS;
  101 } /* main */
  102