Computer Programming Contest Preparation

ToolBox - Source for: 103/10323/a.c



/home/toolbox/public_html/solutions/103/10323/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: 2025-04-09
   19  * Purpose: fun
   20  * Problem: 10323 - Factorial! You Must be Kidding!!!
   21  */
   22 
   23 /*
   24  * This template reads data until a terminating value is reached.
   25  */
   26 
   27 int n;
   28 long int fac[14];
   29 
   30 void init()
   31 {
   32     /* FUNCTION init */
   33     /* Factorials
   34     1  1
   35     2  2
   36     3  6
   37     4  24
   38     5  120
   39     6  720
   40     7  5040
   41        loweer limit
   42     8  40320
   43     9  362880
   44     10 3628800
   45     11 39916800
   46     12 479001600
   47     13 6227020800
   48        upper limit
   49     */
   50     fac[8] = 40320;
   51     fac[9] = 362880;
   52     fac[10] = 3628800;
   53     fac[11] = 39916800;
   54     fac[12] = 479001600;
   55     fac[13] = 6227020800;
   56 } /* FUNCTION init */
   57 
   58 void dump()
   59 {
   60     /* FUNCTION dump */
   61 } /* FUNCTION dump */
   62 
   63 int getInput()
   64 {
   65     /* FUNCTION getInput */
   66     int dataReadFlag;
   67 
   68     dataReadFlag = 1 == scanf(" %d ", &n);
   69     /* hack to deal with negative numbers */
   70     /*
   71     if (0 > n) { n = 0 - n; }
   72     */
   73     return (dataReadFlag);
   74 } /* FUNCTION getInput */
   75 
   76 void process()
   77 {
   78     /* FUNCTION process */
   79     /* refer to https://algorithmist.com/wiki/UVa_10323_-_Factorial!_You_Must_be_Kidding!!!
   80      * to explain the first to if tests for negative numbers
   81      */
   82     if((0 > n) && (0 == n%2))
   83         {
   84             /* negative even */
   85             printf("Underflow!\n");
   86         } /* negative even */
   87     else if((0 > n) && (0 !=n%2))
   88         {
   89             /* negative odd */
   90             printf("Overflow!\n");
   91         } /* negative odd */
   92     else if (8 > n)
   93         {
   94             /* underflow */
   95             printf("Underflow!\n");
   96         } /* underflow */
   97     else if (13 < n)
   98         {
   99             /* overflow */
  100             printf("Overflow!\n");
  101         } /* overflow */
  102     else
  103         {
  104             /* print factorial */
  105             printf("%ld\n", fac[n]);
  106         } /* print factorial */
  107 } /* FUNCTION process */
  108 
  109 int main()
  110 {
  111     /* main */
  112     int moreToDo;
  113 
  114     init();
  115     moreToDo = getInput();
  116     while (moreToDo)
  117         {
  118             /* while */
  119             process();
  120             moreToDo = getInput();
  121         } /* while */
  122 
  123     return EXIT_SUCCESS;
  124 } /* main */
  125