Computer Programming Contest Preparation

ToolBox - Source for: 6/619/w.c



/home/toolbox/public_html/solutions/6/619/w.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: 2013-01-25
   18  * Purpose: fun
   19  * Problem: 619 - Numerically Speaking
   20  */
   21 
   22 /*
   23  * This template reads data until a terminating value is reached.
   24  */
   25 
   26 #define BASE 26
   27 #define BUCKETS 40
   28 #define MAX_VALUE 999
   29 #define NUM_DIGITS 3
   30 #define MAX_BUFFER_LENGTH 40
   31 
   32 /* global variables */
   33 int num[BUCKETS+1];
   34 char buff[MAX_BUFFER_LENGTH];
   35 
   36 /* start code */
   37 void init()
   38 {
   39     /* FUNCTION init */
   40     int i;
   41 
   42     for (i=0; i<=BUCKETS; i++)
   43         {
   44             /* for each bucket */
   45             num[i] = 0;
   46         } /* for each bucket */
   47 } /* FUNCTION init */
   48 
   49 void addDigit(int digit, int base)
   50 {
   51     /* FUNCTION addDigit */
   52     int i;
   53     int carry;
   54 
   55     num[0] = num[0] + digit;
   56     for (i=0; i<BUCKETS; i++)
   57         {
   58             /* for each bucket */
   59             if (MAX_VALUE < num[i])
   60                 {
   61                     /* we have a carry */
   62                     carry = num[i] / (MAX_VALUE + 1);
   63                     num[i] = num[i] % (MAX_VALUE + 1);
   64                     num[i+1] = num[i+1] + carry;
   65                 } /* we have a carry */
   66         } /* for each bucket */
   67 } /* FUNCTION addDigit */
   68 
   69 void dumpNum()
   70 {
   71     /* FUNCTION dumpNum */
   72     int i;
   73     int comma = FALSE;
   74 
   75     for (i=(BUCKETS-1); 0<=i; i--)
   76         {
   77             /* for */
   78             if (comma)
   79                 {
   80                     /* print regardless of value */
   81                     printf("%03d", num[i]);
   82                 } /* print regardless of value */
   83             else if (0 < num[i])
   84                 {
   85                     /* time to start printing */
   86                     printf("%d", num[i]);
   87                     comma = TRUE;
   88                 } /* time to start printing */
   89         } /* for */
   90 } /* FUNCTION dumpNum */
   91 
   92 void process()
   93 {
   94     /* FUNCTION process */
   95     int t = 1;
   96 
   97     dumpNum();
   98     printf("\n");
   99     addDigit(t, BASE);
  100 } /* FUNCTION process */
  101 
  102 int main ()
  103 {
  104     /* main */
  105 
  106     /* 20,725,274,851,017,785,518,433,805,270 */
  107     init();
  108     while (0 == num[9])
  109         {
  110             /* while */
  111             process();
  112         } /* while */
  113 
  114     return EXIT_SUCCESS;
  115 } /* main */
  116 
  117