Computer Programming Contest Preparation

ToolBox - Source for: 100/10013/a.c



/home/toolbox/public_html/solutions/100/10013/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 <stdlib.h>
    7 #include <math.h>
    8 #include <stdint.h>
    9 
   10 #define TRUE  (1 == 1)
   11 #define FALSE (1 != 1)
   12 
   13 #define DEBUG if (FALSE)
   14 
   15 /* fprintf(stderr, "functionName: message", varslist); */
   16 
   17 /*
   18  *  Author: Isaac Traxler
   19  *    Date: 2020-02-05
   20  * Purpose: fun
   21  * Problem: 10013
   22  */
   23 
   24 /*
   25  * This template reads data a specified number of times.
   26  */
   27 
   28 #define MAX_LENGTH 1000010
   29 
   30 int numberOfTimes;
   31 int cnt;
   32 char ans[MAX_LENGTH];
   33 int a;
   34 int b;
   35 
   36 void init()
   37 {
   38     /* FUNCTION init */
   39     scanf("%d ", &numberOfTimes);
   40 } /* FUNCTION init */
   41 
   42 void dump()
   43 {
   44     /* FUNCTION dump */
   45 } /* FUNCTION dump */
   46 
   47 void getInput()
   48 {
   49     /* FUNCTION getInput */
   50     scanf(" %d ", &cnt);
   51 } /* FUNCTION getInput */
   52 
   53 void process()
   54 {
   55     /* FUNCTION process */
   56     int i;
   57     int overflow = FALSE;
   58 
   59     for (i=0; cnt>i; i++)
   60         {
   61             /* do each sum */
   62             scanf(" %d %d ", &a, &b);
   63             ans[i] = a + b;
   64         } /* do each sum */
   65     ans[i] = 0;  /* add string termination */
   66     /* now hndle carries */
   67     for (i=cnt-1; 0<i; i--)
   68         {
   69             /* do each digit backwards */
   70             if (9 < ans[i])
   71                 {
   72                     /* carry case */
   73                     ans[i-1] = ans[i-1] + 1;
   74                     ans[i] = ans[i] - 10;
   75                 } /* carry case */
   76             /* Now convert digit to printable digit */
   77             ans[i] = ans[i] + '0';
   78         } /* do each digit backwards */
   79     if (9 < ans[0])
   80         {
   81             /* overflow that was promised not to happen */
   82             ans[0] = ans[0] - 10;
   83             printf("1");
   84         } /* overflow that was promised not to happen */
   85     ans[0] = ans[0] +'0';
   86     printf("%s\n", ans);
   87 
   88 } /* FUNCTION process */
   89 
   90 int main()
   91 {
   92     /* main */
   93     int i;
   94 
   95     init();
   96     for (i=0; i<numberOfTimes; i++)
   97         {
   98             /* while */
   99             getInput();
  100             if (0 < i)
  101                 {
  102                     printf("\n");
  103                 }
  104             process();
  105         } /* while */
  106 
  107     return EXIT_SUCCESS;
  108 } /* main */
  109 
  110