Computer Programming Contest Preparation

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



/home/toolbox/public_html/solutions/8/893/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  *  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 int leapYear(int year)
   41 {
   42     /* FUNCTION leapYear */
   43     return ((0 == (year % 4)) && (0 != (year % 100)) || (0 == (year % 400)));
   44 } /* FUNCTION leapYear */
   45 
   46 void init()
   47 {
   48     /* FUNCTION init */
   49     int i;
   50     int cnt = 0;
   51     int tot = 0;
   52     int j;
   53 
   54     tbl[0] = 0;    /* 1998 */
   55     year = 1997;
   56 
   57     for (i=1; MAX_SIZE>i; i=i+1)
   58         {
   59             /* for each year */
   60             tot = tot + 365;
   61             if (leapYear(year+i))
   62                 {
   63                     tot = tot + 1;
   64                 }
   65             tbl[i] = tot;
   66             DEBUG printf("tbl[%d] = %d\n", year + i + 1, tot);
   67         } /* for each year */
   68 
   69 } /* FUNCTION init */
   70 
   71 void dump()
   72 {
   73     /* FUNCTION dump */
   74 } /* FUNCTION dump */
   75 
   76 int getInput()
   77 {
   78     /* FUNCTION getInput */
   79     int dataReadFlag;
   80 
   81     scanf(" %d %d%d %d ", &offset, &day, &month, &year);
   82     dataReadFlag = (0 != year);
   83     return (dataReadFlag);
   84 } /* FUNCTION getInput */
   85 
   86 int calcDays(int year, int month, int day)
   87 {
   88     /* FUNCTION calcDays */
   89     int tot;
   90 
   91     DEBUG printf("(year = %d)  (month = %d %d-%d)  (day = %d)\n", year, month, norm[month], leap[month], day);
   92     if (leapYear(year))
   93         {
   94             /* leap year */
   95             tot = leap[month] + day;
   96         } /* leap year */
   97     else
   98         {
   99             /* normal year */
  100             tot = norm[month] + day;
  101         } /* normal year */
  102     return tot;
  103 } /* FUNCTION calcDays */
  104 
  105 int determineMonth(int days, int year)
  106 {
  107     /* FUNCTION determineMonth */
  108     int i = 1;
  109 
  110     if (leapYear(year))
  111         {
  112             /* leap year */
  113             while (days > leap[i])
  114                 {
  115                     i++;
  116                 }
  117             i--;
  118             day = days - leap[i];
  119         } /* leap year */
  120     else
  121         {
  122             /* not a leap year */
  123             while (days > norm[i])
  124                 {
  125                     i++;
  126                 }
  127             i--;
  128             day = days - norm[i];
  129         } /* not a leap year */
  130     return (i);
  131 } /* FUNCTION determineMonth */
  132 
  133 void process()
  134 {
  135     /* FUNCTION process */
  136     int tyear;
  137     int tot;
  138     int tmp;
  139 
  140     tyear = year - 1998; /* deal with offset becasue I was to cheap to waste 1998 locations */
  141     tot = tbl[tyear] + calcDays(year, month, day);
  142     /* now wehave days since beginning of time (jan 1 1998) */
  143     DEBUG printf("(tot = %d) (offset = %d)\n", tot, offset);
  144     tot = tot + offset;
  145     tmp = 10 + (offset / 365) + tyear;
  146     while (tbl[tmp] > (tot -1))
  147         {
  148             tmp--;
  149         }
  150     year = 1998 + tmp;
  151     tmp = tot - tbl[tmp];
  152     month = determineMonth(tmp, year);
  153     DEBUG printf("%d %d\n", year, tmp);
  154     printf("%d %d %d\n", day, month, year);
  155 } /* FUNCTION process */
  156 
  157 int main()
  158 {
  159     /* main */
  160     int moreToDo;
  161 
  162     init();
  163     moreToDo = getInput();
  164     while (moreToDo)
  165         {
  166             /* while */
  167             process();
  168             moreToDo = getInput();
  169         } /* while */
  170 
  171     return EXIT_SUCCESS;
  172 } /* main */
  173