Computer Programming Contest Preparation

ToolBox - Source for: 5/514/b.c



/home/toolbox/public_html/solutions/5/514/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: 2015-03-30
   18  * Purpose:
   19  * Problem: 514
   20  */
   21 
   22 /*
   23  * This template reads data until a terminating value is reached.
   24  */
   25 
   26 #define STACK_SIZE 1100
   27 
   28 int stack[STACK_SIZE];
   29 int top;
   30 
   31 int cars;
   32 
   33 void init()
   34 {
   35     /* FUNCTION init */
   36 } /* FUNCTION init */
   37 
   38 void dump()
   39 {
   40     /* FUNCTION dump */
   41 } /* FUNCTION dump */
   42 
   43 int inspect()
   44 {
   45     /* FUNCTION inspect */
   46     int toReturn;
   47 
   48     if (0 == top)
   49         {
   50             /* stack empty */
   51             toReturn = -1;
   52         } /* stack empty */
   53     else
   54         toReturn = stack[top];
   55     return toReturn;
   56 } /* FUNCTION inspect */
   57 
   58 int pop()
   59 {
   60     /* FUNCTION pop */
   61     return stack[top--];
   62 } /* FUNCTION pop */
   63 
   64 void push(int x)
   65 {
   66     /* FUNCTION push */
   67     stack[top++] = x;
   68 } /* FUNCTION push */
   69 
   70 int getInput()
   71 {
   72     /* FUNCTION getInput */
   73     int dataReadFlag;
   74 
   75     scanf(" %d ", &cars);
   76     dataReadFlag = (0 != cars);
   77     return (dataReadFlag);
   78 } /* FUNCTION getInput */
   79 
   80 void process()
   81 {
   82     /* FUNCTION process */
   83     int car;
   84     int nxt;
   85     int success = TRUE;
   86 
   87     car = 1;
   88     top = 0;
   89     scanf(" %d ", &nxt);
   90     if (0 != nxt)
   91         {
   92             /* have a case */
   93             while (car <= cars)
   94                 {
   95                     /* while */
   96                     printf("car = %d  nxt = %d  top = %d\n", car, nxt, top);
   97                     if (car == nxt)
   98                         {
   99                             /* car is the next one wanted */
  100                             car++;
  101                             scanf(" %d ", &nxt);
  102                         } /* car is the next one wanted */
  103                     else if (car > nxt)
  104                         {
  105                             /* desired car must be on stack */
  106                             if (nxt == inspect())
  107                                 {
  108                                     /* car on top is one we want */
  109                                     pop();
  110                                 } /* car on top is one we want */
  111                             else
  112                                 {
  113                                     /* not here -- impossible */
  114                                     success = FALSE;
  115                                     while (car < cars)
  116                                         {
  117                                             /* clear rest of cars */
  118                                             scanf(" %d ", &nxt);
  119                                             car++;
  120                                         } /* clear rest of cars */
  121                                 } /* not here -- impossible */
  122 
  123                         } /* desired car must be on stack */
  124                     else
  125                         {
  126                             /* car < nxt */
  127                             while (car <= nxt)
  128                                 {
  129                                     /* while */
  130                                     push(car);
  131                                     car++;
  132                                 } /* while */
  133                         } /* car < nxt */
  134                 } /* while */
  135         } /* have a case */
  136     if (success)
  137         {
  138             printf("Yes");
  139         }
  140     else
  141         {
  142             printf("No");
  143         }
  144     printf("\n");
  145 } /* FUNCTION process */
  146 
  147 int main()
  148 {
  149     /* main */
  150     int moreToDo;
  151 
  152     init();
  153     moreToDo = getInput();
  154     while (moreToDo)
  155         {
  156             /* while */
  157             process();
  158             moreToDo = getInput();
  159         } /* while */
  160 
  161     return EXIT_SUCCESS;
  162 } /* main */
  163 
  164 
  165 
  166 
  167