Computer Programming Contest Preparation

ToolBox - Source for: 100/10038/b.c



/home/toolbox/public_html/solutions/100/10038/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 #define MAX_ITEMS 3005
   15 
   16 /*
   17  *  Author: Josh Abadie/Isaac Traxler
   18  *    Date: 2008 03 03
   19  * Purpose:
   20  * Problem: 10038 - Jolly Jumpers
   21  */
   22 
   23 /*
   24 
   25   possible optimizations:
   26   1) Make flags an array of bytes (reduce storage, may increase runtime)
   27 
   28 */
   29 
   30 int count;
   31 int flags[MAX_ITEMS];
   32 
   33 void init()
   34 {
   35     /* FUNCTION init */
   36     int i;
   37 
   38     for (i=0; i<count; i++)
   39         {
   40             flags[i] = 0;
   41         }
   42 } /* FUNCTION init */
   43 
   44 void dump()
   45 {
   46     /* FUNCTION dump */
   47 } /* FUNCTION dump */
   48 
   49 int getInput()
   50 {
   51     /* FUNCTION getInput */
   52     int dataReadFlag = TRUE;
   53     int i;
   54 
   55     if (EOF == scanf(" %d ", &count))
   56         {
   57             /* then */
   58             dataReadFlag = FALSE;
   59         } /* then */
   60     else
   61         {
   62             /* else */
   63             init();
   64         } /* else */
   65 
   66     return (dataReadFlag);
   67 } /* FUNCTION getInput */
   68 
   69 void process()
   70 {
   71     /* FUNCTION process */
   72     int i;
   73     int tmp;
   74     int res = TRUE;
   75     int curr, prev;
   76 
   77     scanf(" %d ",&prev);
   78     for (i = 1; (i < (count)); i ++)
   79         {
   80             /* for */
   81             scanf(" %d ",&curr);
   82             if (res)
   83                 {
   84                     /* Failure.  Read in rest of input */
   85                     tmp = abs(curr - prev);
   86                     if ((0 != tmp) && (count > tmp) && (0 == flags[tmp]))
   87                         {
   88                             /* valid */
   89                             flags[tmp] = 1;
   90                         } /* valid */
   91                     else
   92                         {
   93                             /* not valid */
   94                             res = FALSE;
   95                         } /* not valid */
   96                     prev = curr;
   97                 } /* Failure.  Read in rest of input */
   98         } /* for */
   99 
  100     if (res)
  101         {
  102             /* success */
  103             printf("Jolly\n");
  104         } /* success */
  105     else
  106         {
  107             /* failure! */
  108             printf("Not jolly\n");
  109         } /* failure! */
  110 } /* FUNCTION process */
  111 
  112 int main ()
  113 {
  114     /* main */
  115     int moreToDo;
  116 
  117     moreToDo = getInput();
  118     while (moreToDo)
  119         {
  120             /* while */
  121             process();
  122             moreToDo = getInput();
  123         } /* while */
  124 
  125     return EXIT_SUCCESS;
  126 } /* main */
  127