Computer Programming Contest Preparation

ToolBox - Source for: 8/893/b.c



/home/toolbox/public_html/solutions/8/893/b.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:
   18  * Purpose: fun
   19  * Problem: 893
   20  */
   21 
   22 /*
   23  * This template reads data until a terminating value is reached.
   24  */
   25 
   26 #define MAX_SIZE ((1003) + (999999999 / 365))
   27 
   28 int tbl[MAX_SIZE + 5];
   29 int day;
   30 int month;
   31 int year;
   32 int offset;
   33 /*
   34 int norm[13] = { 0, 0, 31, 28, 31,  30,  31,  30,  31,  31,  30,  31,  30,  31};
   35 */
   36 /* waste 0th location so I do not have to subtract 1 from month */
   37 int norm[14] = { 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
   38 int leap[14] = { 0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366};
   39 
   40 void init()
   41 {
   42     /* FUNCTION init */
   43     int i;
   44     int cnt = 0;
   45     int tot;
   46     int j;
   47 
   48     tbl[0] = 0;    /* 1998 */
   49     tbl[1] = 365;  /* 1999 */
   50     tbl[2] = 730;  /* 2000 */
   51     tbl[3] = 1096; /* 2001 */
   52     tot = 1096 + 365; /* 2000 was a leap year */
   53 
   54     for (i=0,j=4; MAX_SIZE>i; i=i+4)
   55         {
   56             /* for each year */
   57             tbl[j++] = tot;
   58             DEBUG printf("tbl[%d] = %d\n", j+1997, tbl[j-1]);
   59             tot = tot + 365;
   60             tbl[j++] = tot;
   61             DEBUG printf("tbl[%d] = %d\n", j+1997, tbl[j-1]);
   62             tot = tot + 365;
   63             tbl[j++] = tot;
   64             DEBUG printf("tbl[%d] = %d\n", j+1997, tbl[j-1]);
   65             tot = tot + 365;
   66             if ((0 != (i % 100)) || (0 == (i % 400)))
   67                 {
   68                     tot++;
   69                 }
   70             tbl[j++] = tot;
   71             DEBUG printf("tbl[%d] = %d\n", j+1997, tbl[j-1]);
   72             tot = tot + 365;
   73         } /* for each year */
   74 
   75 } /* FUNCTION init */
   76 
   77 void dump()
   78 {
   79     /* FUNCTION dump */
   80 } /* FUNCTION dump */
   81 
   82 int leapYear(int year)
   83 {
   84     /* FUNCTION leapYear */
   85     return ((0 == (year % 4)) && (0 != (year % 100)) || (0 == (year % 400)));
   86 } /* FUNCTION leapYear */
   87 
   88 int getInput()
   89 {
   90     /* FUNCTION getInput */
   91     int dataReadFlag;
   92 
   93     scanf(" %d %d%d %d ", &offset, &day, &month, &year);
   94     dataReadFlag = (0 != year);
   95     return (dataReadFlag);
   96 } /* FUNCTION getInput */
   97 
   98 int calcDays(int year, int month, int day)
   99 {
  100     /* FUNCTION calcDays */
  101     int tot;
  102 
  103     DEBUG printf("(year = %d)  (month = %d %d-%d)  (day = %d)\n", year, month, norm[month], leap[month], day);
  104     if (leapYear(year))
  105         {
  106             /* leap year */
  107             tot = leap[month] + day;
  108         } /* leap year */
  109     else
  110         {
  111             /* normal year */
  112             tot = norm[month] + day;
  113         } /* normal year */
  114     return tot;
  115 } /* FUNCTION calcDays */
  116 
  117 void process()
  118 {
  119     /* FUNCTION process */
  120     int tyear;
  121     int tot;
  122     int tmp;
  123 
  124     tyear = year - 1998; /* deal with offset becasue I was to cheap to waste 1998 locations */
  125     tot = tbl[tyear] + calcDays(year, month, day);
  126     /* now wehave days since beginning of time (jan 1 1998) */
  127     printf("(tot = %d) (offset = %d)\n", tot, offset);
  128     tot = tot + offset;
  129     tmp = 1 + (offset / 365) + tbl[tyear];
  130     while (tbl[tmp] > tot)
  131         {
  132             tmp--;
  133         }
  134     year = year + tmp;
  135     tmp = tot - tbl[tmp];
  136     printf("%d %d\n", year, tmp);
  137 } /* FUNCTION process */
  138 
  139 int main()
  140 {
  141     /* main */
  142     int moreToDo;
  143 
  144     init();
  145     moreToDo = getInput();
  146     while (moreToDo)
  147         {
  148             /* while */
  149             process();
  150             moreToDo = getInput();
  151         } /* while */
  152 
  153     return EXIT_SUCCESS;
  154 } /* main */
  155