Computer Programming Contest Preparation

ToolBox - Source for: 102/10220/a.c



/home/toolbox/public_html/solutions/102/10220/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 /*
   16  *  Author: Isaac Traxler
   17  *    Date: 2015-10-04
   18  * Purpose: fun
   19  * Problem: 10220 - I Love Big Numbers!
   20  */
   21 
   22 /*
   23  * This template reads data until a terminating value is reached.
   24  */
   25 
   26 #define MAX_DIGITS 3000
   27 #define MAX_NUMBERS 1001
   28 
   29 int num;
   30 int numbers[MAX_NUMBERS] = { 0 };
   31 int n[MAX_DIGITS] = { 0 };
   32 int dgtCnt = 0;
   33 int tmp;
   34 
   35 void mult(int x)
   36 {
   37     /* FUNCTION mult */
   38     int i;
   39     int carry = 0;
   40 
   41     for (i=0; i<=dgtCnt; i++)
   42         {
   43             /* process each digit */
   44             tmp = (n[i] * x) + carry;
   45             carry = 0;
   46             if (9 < tmp)
   47                 {
   48                     /* carry case */
   49                     carry = tmp / 10;
   50                     tmp = tmp % 10;
   51                 } /* carry case */
   52             n[i] = tmp;
   53         } /* process each digit */
   54     /* carry may still have up to 5 digits */
   55     while (0 < carry)
   56         {
   57             /* while */
   58             dgtCnt++;
   59             n[dgtCnt] = carry % 10;
   60             carry = carry / 10;
   61         } /* while */
   62 } /* FUNCTION mult */
   63 
   64 int sum()
   65 {
   66     /* FUNCTION sum */
   67     int i;
   68     int tot = 0;
   69 
   70     for (i=0; i<=dgtCnt; i++)
   71         {
   72             /* for each digit */
   73             tot = tot + n[i];
   74         } /* for each digit */
   75     return tot;
   76 } /* FUNCTION sum */
   77 
   78 void dump(int x)
   79 {
   80     /* FUNCTION dump */
   81     int i;
   82 
   83     for (i=dgtCnt; 0<=i; i--)
   84         {
   85             printf("%d", n[i]);
   86         }
   87     printf("    [%d] = %d\n", x, numbers[x]);
   88 } /* FUNCTION dump */
   89 
   90 void init()
   91 {
   92     /* FUNCTION init */
   93     int i;
   94 
   95     numbers[0] = 1;
   96     numbers[1] = 1;
   97     n[0] = 1;
   98 
   99     for (i=2; i<MAX_NUMBERS; i++)
  100         {
  101             /* for each number to do factorial of */
  102             mult(i);
  103             numbers[i] = sum();
  104             DEBUG dump(i);
  105         } /* for each number to do factorial of */
  106 } /* FUNCTION init */
  107 
  108 int getInput()
  109 {
  110     /* FUNCTION getInput */
  111     int dataReadFlag;
  112 
  113     dataReadFlag = 0 < scanf(" %d ", &num);
  114     return (dataReadFlag);
  115 } /* FUNCTION getInput */
  116 
  117 void process()
  118 {
  119     /* FUNCTION process */
  120     printf("%d\n", numbers[num]);
  121 } /* FUNCTION process */
  122 
  123 int main()
  124 {
  125     /* main */
  126     int moreToDo;
  127 
  128     init();
  129     moreToDo = getInput();
  130     while (moreToDo)
  131         {
  132             /* while */
  133             process();
  134             moreToDo = getInput();
  135         } /* while */
  136 
  137     return EXIT_SUCCESS;
  138 } /* main */
  139