Computer Programming Contest Preparation

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



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