Computer Programming Contest Preparation

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



/home/toolbox/public_html/solutions/100/10070/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 #define MAX_LINE 2048
   16 
   17 /*
   18  *  Author: Isaac Traxler
   19  *    Date: 2016-04-11
   20  * Purpose: fun
   21  * Problem: 10070
   22  */
   23 
   24 /*
   25  * This template reads lines of data at a time until end of file.
   26  */
   27 
   28 char line[MAX_LINE];
   29 int sign[2] = { 1, -1 };
   30 
   31 void init()
   32 {
   33     /* FUNCTION init */
   34 } /* FUNCTION init */
   35 
   36 void dump()
   37 {
   38     /* FUNCTION dump */
   39 } /* FUNCTION dump */
   40 
   41 int getInput()
   42 {
   43     /* FUNCTION getInput */
   44     int dataReadFlag;
   45 
   46     fgets(line, MAX_LINE, stdin);
   47     if (feof(stdin))
   48         dataReadFlag = FALSE;
   49     else
   50         {
   51             /* something to read */
   52             dataReadFlag = TRUE;
   53             line[strlen(line)-1] = 0;
   54         } /* something to read */
   55     return (dataReadFlag);
   56 } /* FUNCTION getInput */
   57 
   58 int sum3()
   59 {
   60     /* FUNCTON sum3 */
   61     int tot = 0;
   62     int i;
   63 
   64     DEBUG    printf("(sum3): strlen(line) = %d  line = [%s]\n", strlen(line), line);
   65     for (i=0; i<strlen(line); i++)
   66         {
   67             /* for */
   68             DEBUG printf("(sum3): tot = [%d]  line[%d] = [%c]\n", tot, i, line[i]);
   69             tot = (tot + line[i] - '0') % 3;
   70         } /* for */
   71     return tot;
   72 } /* FUNCTON sum3 */
   73 
   74 int sum11()
   75 {
   76     /* FUNCTON sum11 */
   77     int tot;
   78     int i;
   79     int flag;
   80 
   81     tot = line[0] - '0';
   82     for (i=1; i<strlen(line); i++)
   83         {
   84             /* for */
   85             flag = sign[i % 2];
   86             tot = (tot + (flag * (line[i] - '0')));
   87         } /* for */
   88     if (0 > tot)
   89         {
   90             tot = - tot;
   91         }
   92     tot = tot % 11;
   93     return tot;
   94 } /* FUNCTON sum11 */
   95 
   96 void process()
   97 {
   98     /* FUNCTION process */
   99     int d4;
  100     int d5;
  101     int d100;
  102     int d400;
  103     int leap;
  104     int hu;
  105     int bu;
  106     int t;
  107     int last2;
  108     int last4;
  109 
  110     t = strlen(line) - 1;
  111     last2 = ((line[t-1] - '0') * 10) + (line[t] -'0');
  112     d4 = 0 == (last2 % 4);
  113     d5 = ('0' == line[t]) || ('5' == line[t]);
  114     DEBUG printf("d5=%d line[%d] = %c\n", d5, t, line[t]);
  115     d100 = ('0' == line[t]) && ('0' == line[t-1]);
  116     last4 = last2 + (100 * (line[t-2] - '0')) + (1000 * (line[t-3] - '0'));
  117     d400 = 0 == (last4 % 400);
  118     leap = d400 || (d4 && ! d100);
  119     hu = d5 && (0 == sum3());;
  120     bu = leap && (0 == sum11());
  121     if ((! hu) && (!leap))
  122         {
  123             /* ordinary */
  124             printf("This is an ordinary year.\n");
  125         } /* ordinary */
  126     else
  127         {
  128             /* not an ordinary year */
  129             if (leap)
  130                 {
  131                     /* leap */
  132                     printf("This is leap year.\n");
  133                 } /* leap */
  134             if (hu)
  135                 {
  136                     /* hu */
  137                     printf("This is huluculu festival year.\n");
  138                 } /* hu */
  139             if (bu)
  140                 {
  141                     /* bu */
  142                     printf("This is bulukulu festival year.\n");
  143                 } /* bu */
  144         } /* not an ordinary year */
  145 } /* FUNCTION process */
  146 
  147 int main()
  148 {
  149     /* main */
  150     int moreToDo;
  151     int flag = FALSE;
  152 
  153     init();
  154     moreToDo = getInput();
  155     while (moreToDo)
  156         {
  157             /* while */
  158             if (flag)
  159                 printf("\n");
  160             else
  161                 flag = TRUE;
  162             process();
  163             moreToDo = getInput();
  164         } /* while */
  165 
  166     return EXIT_SUCCESS;
  167 } /* main */
  168