Computer Programming Contest Preparation

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



/home/toolbox/public_html/solutions/5/575/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 #define MAX_DIGITS 100
   16 
   17 /*
   18  *  Author: Isaac Traxler
   19  *    Date: 2015-02-10
   20  * Purpose:
   21  * Problem: 575
   22  */
   23 
   24 /*
   25  * This template reads data until a terminating value is reached.
   26  */
   27 
   28 int cnt;
   29 int digits[MAX_DIGITS];
   30 
   31 void init()
   32 {
   33     /* FUNCTION init */
   34 } /* FUNCTION init */
   35 
   36 void dump()
   37 {
   38     /* FUNCTION dump */
   39 } /* FUNCTION dump */
   40 
   41 int getInput()
   42 {
   43     /* FUNCTION getInput */
   44     int dataReadFlag;
   45     int i;
   46     int j;
   47     char buff[MAX_DIGITS];
   48 
   49     scanf("%s ", buff);
   50     DEBUG printf("[%s]\n", buff);
   51     if (0 == strcmp("0", buff))
   52         {
   53             /* end found */
   54             dataReadFlag = FALSE;
   55         } /* end found */
   56     else
   57         {
   58             /* data found */
   59             dataReadFlag = TRUE;
   60             cnt = strlen(buff);
   61             for (i=cnt-1,j=0; -1<i; i--,j++)
   62                 {
   63                     /* move each digit */
   64                     digits[j] = buff[i] - '0';
   65                 } /* move each digit */
   66         } /* data found */
   67     return (dataReadFlag);
   68 } /* FUNCTION getInput */
   69 
   70 void process()
   71 {
   72     /* FUNCTION process */
   73     int i;
   74     unsigned int total = 0;
   75     unsigned int two;
   76 
   77     two =2;
   78     for (i=0; i<cnt; i++)
   79         {
   80             /* process each digit */
   81             total = total + digits[i]*(two - 1);
   82             DEBUG printf("total=%d  digits[%d]=%d two=%d\n", total, i, digits[i], two);
   83             two = two * 2;
   84         } /* process each digit */
   85     printf("%d\n", total);
   86 } /* FUNCTION process */
   87 
   88 int main()
   89 {
   90     /* main */
   91     int moreToDo;
   92 
   93     init();
   94     moreToDo = getInput();
   95     while (moreToDo)
   96         {
   97             /* while */
   98             process();
   99             moreToDo = getInput();
  100         } /* while */
  101 
  102     return EXIT_SUCCESS;
  103 } /* main */
  104