Computer Programming Contest Preparation

ToolBox - Source for: 5/573/a.c



/home/toolbox/public_html/solutions/5/573/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: 2015-03-23
   18  * Purpose:
   19  * Problem: 573
   20  */
   21 
   22 /*
   23  * This template reads data until a terminating value is reached.
   24  */
   25 
   26 int h;    /* height of well */
   27 int u;    /* initial climb capability */
   28 int d;    /* haw far the snail slides */
   29 int f;    /* fatigue factor */
   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 = TRUE;
   45 
   46     scanf (" %d ", &h);
   47     if (0 < h)
   48         {
   49             /* more data to read */
   50             scanf (" %d %d %d ", &u, &d, &f);
   51         } /* more data to read */
   52     else
   53         {
   54             /* time to quit */
   55             dataReadFlag = FALSE;
   56         } /* time to quit */
   57     return (dataReadFlag);
   58 } /* FUNCTION getInput */
   59 
   60 void process()
   61 {
   62     /* FUNCTION process */
   63     double cur;     /* snails current height */
   64     double loss;    /* how much less he climb every day */
   65     double climb;   /* how high snail can climb today */
   66     int days = 0;   /* how many days it takes */
   67 
   68     cur = 0;
   69     climb = u;
   70     loss = (climb * f) / 100.0;
   71     while ((cur < h) && (cur >= 0))
   72         {
   73             /* while still in well with hope of getting out */
   74             days++;
   75             cur = cur + climb;
   76             if (cur <= h)
   77                 {
   78                     /* did not make it out */
   79                     cur = cur - d;
   80                     climb = climb - loss;
   81                     if (0 > climb)
   82                         {
   83                             climb = 0;
   84                         }
   85                 } /* did not make it out */
   86         } /* while still in well with hope of getting out */
   87     if (0 > cur)
   88         {
   89             /* snail will never get out */
   90             printf("failure");
   91         } /* snail will never get out */
   92     else
   93         {
   94             /* he made it */
   95             printf("success");
   96         } /* he made it */
   97     printf(" on day %d\n", days);
   98 } /* FUNCTION process */
   99 
  100 int main()
  101 {
  102     /* main */
  103     int moreToDo;
  104 
  105     init();
  106     moreToDo = getInput();
  107     while (moreToDo)
  108         {
  109             /* while */
  110             process();
  111             moreToDo = getInput();
  112         } /* while */
  113 
  114     return EXIT_SUCCESS;
  115 } /* main */
  116