Computer Programming Contest Preparation

ToolBox - Source for: 126/12640/d.c



/home/toolbox/public_html/solutions/126/12640/d.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 DEBUG1 if (FALSE)
   14 #define DEBUG if (TRUE)
   15 
   16 
   17 /*
   18  *  Author: Isaac Traxler
   19  *    Date: 2015-10-06
   20  * Purpose: fun
   21  * Problem: 12640 - Largest Sum Game (worst part is input)
   22  */
   23 
   24 /*
   25  * Hard part here is processing it as a line at a time
   26  */
   27 
   28 /*
   29  * This template reads lines of data at a time until end of file.
   30  */
   31 
   32 #define DASH '-'
   33 #define SPACE ' '
   34 
   35 int endOfLine;
   36 int endOfFile = FALSE;
   37 
   38 int getNum()
   39 {
   40     /* FUNCTION getNum */
   41     int num = 0;
   42     int neg = FALSE;
   43     int chr;
   44 
   45     chr = getchar();
   46     endOfFile == feof(stdin);
   47     if (endOfFile)
   48         {
   49             endOfLine = TRUE;
   50             printf("end of file found\n");
   51         }
   52     else
   53         {
   54             /* not end of file */
   55             DEBUG printf("skipping: [");
   56             while ((! isdigit(chr)) && (DASH != chr))
   57                 {
   58                     /* skip any leading whitespace */
   59                     DEBUG1 printf("%c", chr);
   60                     chr = getchar();
   61                 } /* skip any leading whitespace */
   62             DEBUG printf("]\n");
   63             /* handle negative sign */
   64             if (DASH == chr)
   65                 {
   66                     /* found a negative sign */
   67                     neg = TRUE;
   68                     chr = getchar();
   69                     DEBUG printf(" [negative number] ");
   70                 } /* found a negative sign */
   71             /* get each digit */
   72             while (isdigit(chr))
   73                 {
   74                     /* found another digit */
   75                     DEBUG printf("%c(%d) num = %d  ", chr, (chr - '0'), num);
   76                     num = (num * 10) + (chr - '0');
   77                     chr = getchar();
   78                 } /* found another digit */
   79             DEBUG printf(" %d\n", num);
   80             if (SPACE == chr)
   81                 {
   82                     /* more numbers on line */
   83                     endOfLine = FALSE;
   84                     endOfFile = FALSE;
   85                 } /* more numbers on line */
   86             else
   87                 {
   88                     /* end of something */
   89                     endOfLine = TRUE;
   90                     endOfFile == feof(stdin);
   91                 } /* end of something */
   92             if (neg)
   93                 {
   94                     num = 0 - num;
   95                 }
   96             DEBUG printf("num = %d\n", num);
   97         } /* not end of file */
   98     return num;
   99 } /* FUNCTION getNum */
  100 
  101 void process()
  102 {
  103     /* FUNCTION process */
  104     int mx = -999999;
  105     int cur = 0;
  106     int tmp;
  107 
  108     printf("Start line\n");
  109     tmp = getNum();
  110     while (! endOfLine)
  111         {
  112             /* something to process */
  113             printf("processing: %d mx=%d cur=%d\n", tmp, mx, cur);
  114             if (tmp > (tmp + cur))
  115                 {
  116                     cur = tmp;
  117                 }
  118             else
  119                 {
  120                     cur = cur + tmp;
  121                 }
  122             if (cur > mx)
  123                 {
  124                     mx = cur;
  125                 }
  126             tmp = getNum();
  127         } /* something to process */
  128     printf("Answer is: ");
  129     printf("%d\n", tmp);
  130 } /* FUNCTION process */
  131 
  132 int main()
  133 {
  134     /* main */
  135 
  136     while (! endOfFile)
  137         {
  138             /* while */
  139             process();
  140         } /* while */
  141 
  142     return EXIT_SUCCESS;
  143 } /* main */
  144