Computer Programming Contest Preparation

ToolBox - Source for: 132/13276/c.c



/home/toolbox/public_html/solutions/132/13276/c.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 <stdlib.h>
    7 #include <math.h>
    8 #include <stdint.h>
    9 
   10 #define TRUE  (1 == 1)
   11 #define FALSE (1 != 1)
   12 
   13 #define DEBUG if (FALSE)
   14 #define INT long
   15 
   16 /* fprintf(stderr, "functionName: message", varslist); */
   17 
   18 /*
   19  *  Author: Isaac Traxler
   20  *    Date: 2018-10-31
   21  * Purpose: fun
   22  * Problem: 13276
   23  */
   24 
   25 /*
   26  * This template reads data a specified number of times.
   27  */
   28 
   29 int numberOfTimes;
   30 int e;  /* initial energy */
   31 int p;  /* damage a round does */
   32 int k;  /* rounds per gun */
   33 int r;  /* energy gain per swap */
   34 INT tmp;
   35 INT pk;
   36 INT rndDamage;
   37 INT rnds;
   38 int cse;
   39 
   40 /* if p*k > e -- stewart will die right off
   41  * if stewart survives first gun
   42  * if (p*k <= r), stewart will never be defeated
   43  */
   44 
   45 void process()
   46 {
   47     /* FUNCTION process */
   48 
   49     pk = p * k;
   50     if (pk >= e)
   51         {
   52             /* stewart will die in first round */
   53             tmp = (e + p - 1) / p;
   54         } /* stewart will die in first round */
   55     else
   56         {
   57             /* stewart makes it to 2nd gun at least */
   58             if (pk <= r)
   59                 {
   60                     /* stewart is immortal */
   61                     tmp = -1;
   62                 } /* stewart is immortal */
   63             else
   64                 {
   65                     /* figure out how many shots it will take to kill stewart */
   66                     /* remove damage done in first round */
   67                     /*
   68                      *  (e + r) / (p * k) is how many more rounds to go
   69                      */
   70                     e = e - pk; /* remove first round */
   71                     rndDamage = pk - r; /* round does pk damage with r regen */
   72                     rnds = (e / rndDamage);
   73                     e = e - (rnds * rndDamage);
   74                     tmp = ((1 + rnds) * k);
   75                     if (0 < e)
   76                         {
   77                             /* a partial round */
   78                             tmp = tmp + ((e + r + p - 1) / p);
   79                         } /* a partial round */
   80                 } /* figure out how many shots it will take to kill stewart */
   81         } /* stewart makes it to 2nd gun at least */
   82 
   83     printf("Case %d: %ld\n", cse, tmp);
   84 } /* FUNCTION process */
   85 
   86 int main()
   87 {
   88     /* main */
   89 
   90     scanf("%d ", &numberOfTimes);
   91     for (cse=1; cse<=numberOfTimes; cse++)
   92         {
   93             /* while */
   94             scanf(" %d %d %d %d ", &e, &p, &k, &r);
   95             process();
   96         } /* while */
   97 
   98     return EXIT_SUCCESS;
   99 } /* main */
  100 
  101