Computer Programming Contest Preparation

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



/home/toolbox/public_html/solutions/126/12640/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 /*
   17  *  Author: Isaac Traxler
   18  *    Date: 2017-09-06
   19  * Purpose: fun
   20  * Problem: 12640 Largest Sum Game
   21  */
   22 
   23 /*
   24  * This template reads lines of data at a time until end of file.
   25  */
   26 
   27 #define MAX_LINE 500005
   28 char line[MAX_LINE];
   29 
   30 void init()
   31 {
   32     /* FUNCTION init */
   33 } /* FUNCTION init */
   34 
   35 void dump()
   36 {
   37     /* FUNCTION dump */
   38 } /* FUNCTION dump */
   39 
   40 int getInput()
   41 {
   42     /* FUNCTION getInput */
   43     int dataReadFlag;
   44 
   45     fgets(line, MAX_LINE, stdin);
   46     if (feof(stdin))
   47         dataReadFlag = FALSE;
   48     else
   49         {
   50             /* something to read */
   51             dataReadFlag = TRUE;
   52             line[strlen(line)-1] = ' ';
   53         } /* something to read */
   54     return (dataReadFlag);
   55 } /* FUNCTION getInput */
   56 
   57 int getNum(char tmp[])
   58 {
   59     /* FUNCTION getNum */
   60     int num = 0;
   61     int i;
   62     int start = 0;
   63     int negative = FALSE;
   64 
   65     DEBUG printf("token = [");
   66     /* deal with possible - negative sign */
   67     if ('-' == tmp[0])
   68         {
   69             negative = TRUE;
   70             start = 1;
   71         }
   72     /* traditional num convert */
   73     for (i=start; strlen(tmp)>i; i++)
   74         {
   75             /* for */
   76             DEBUG printf("%c", tmp[i]);
   77             num = (10 * num) + tmp[i] - '0';
   78         } /* for */
   79     DEBUG printf("]\n");
   80     if (negative)
   81         {
   82             num = - num;
   83         }
   84     return num;
   85 } /* FUNCTION getNum */
   86 
   87 void process()
   88 {
   89     /* FUNCTION process */
   90     int mx = 0;
   91     int cur = 0;
   92     int num;
   93     char *p;
   94 
   95     p = strtok(line, " ");
   96     while (NULL != p)
   97         {
   98             /* while */
   99             num = getNum(p);
  100             DEBUG printf("%d ", num);
  101             if ((num+cur) > num)
  102                 {
  103                     /* running sum is bigger */
  104                     cur = cur + num;
  105                 } /* running sum is bigger */
  106             else
  107                 {
  108                     /* better off starting over */
  109                     cur = num;
  110                 } /* better off starting over */
  111             if (cur > mx)
  112                 {
  113                     mx = cur;
  114                 }
  115             p = strtok(NULL, " ");
  116         } /* while */
  117 
  118     DEBUG printf("Answer is: ");
  119     printf("%d\n", mx);
  120 
  121 
  122 } /* FUNCTION process */
  123 
  124 int main()
  125 {
  126     /* main */
  127     int moreToDo;
  128 
  129     init();
  130     moreToDo = getInput();
  131     while (moreToDo)
  132         {
  133             /* while */
  134             process();
  135             moreToDo = getInput();
  136         } /* while */
  137 
  138     return EXIT_SUCCESS;
  139 } /* main */
  140