Computer Programming Contest Preparation

ToolBox - Source for: 3/305/b.c



/home/toolbox/public_html/solutions/3/305/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: 2021-01-28
   18  * Purpose: fun
   19  * Problem: 305 Joseph
   20  */
   21 
   22 /*
   23  * This template reads data until a terminating value is reached.
   24  */
   25 
   26 #define MAX_NUM 15
   27 #define MAXX 30
   28 
   29 int buf[MAXX];
   30 int k;
   31 
   32 
   33 void init(int x)
   34 {
   35     /* FUNCTION init */
   36     int i;
   37 
   38     for (i=0; i<x; i++)
   39         {
   40             buf[i] = i + 1;
   41         }
   42 } /* FUNCTION init */
   43 
   44 void dump(int t)
   45 {
   46     /* FUNCTION dump */
   47     int i;
   48 
   49     for (i=0; i<t; i++)
   50         {
   51             printf("%d ", buf[i]);
   52         }
   53     printf("\n");
   54 } /* FUNCTION dump */
   55 
   56 int getInput()
   57 {
   58     /* FUNCTION getInput */
   59     int dataReadFlag;
   60     scanf(" %d ", &k);
   61     dataReadFlag = 0 < k;
   62     return (dataReadFlag);
   63 } /* FUNCTION getInput */
   64 
   65 int allGood(int t)
   66 {
   67     /* FUNCTION allGood */
   68     int i;
   69     int tot;
   70     int good = FALSE;
   71 
   72     tot = 0;
   73     for (i=0; i<k; i++)
   74         {
   75             /* for good */
   76             if (0 < buf[i])
   77                 {
   78                     tot = tot + 1;
   79                 }
   80         } /* for good */
   81     if (k == tot)
   82         {
   83             /* check that all bad are dead */
   84             tot = 0;
   85             for (i=k; i<t; i++)
   86                 {
   87                     /* for bad */
   88                     if (0 == buf[i])
   89                         {
   90                             tot = tot + 1;
   91                         }
   92                 } /* for bad */
   93             good = (k == tot);
   94         } /* check that all bad are dead */
   95     return good;
   96 } /* FUNCTION allGood */
   97 
   98 int testIt(int v, int t)
   99 {
  100     /* FUNCTION testIt */
  101     int i;
  102     int p;
  103     int x;
  104     int good;
  105     int peopleLeft;
  106 
  107     peopleLeft = t;
  108     p = ((v - 1 + t)% t);
  109     for (i=0; i<k; i++)
  110         {
  111             /* kill k times */
  112             buf[p] = 0;
  113             peopleLeft = peopleLeft - 1;
  114             DEBUG printf("Killing %d\n", p + 1);
  115             x = 1;
  116             while (x <= v)
  117                 {
  118                     /* while */
  119                     p = (p + 1) % t;
  120                     if (0 < buf[p])
  121                         {
  122                             /* found a live person */
  123                             x = x + 1;
  124                             DEBUG printf("Skipping %d\n", p + 1);
  125                         } /* found a live person */
  126                 } /* while */
  127             if (p < k)
  128                 {
  129                     i = k;
  130                 }
  131         } /* kill k times */
  132     good = allGood(t);
  133 } /* FUNCTION testIt */
  134 
  135 void process()
  136 {
  137     /* FUNCTION process */
  138     int t;
  139     int i;
  140     int failed = TRUE;
  141     int tst;
  142 
  143     printf("process %d\n", k);
  144     t = k * 2;
  145     tst = k;
  146     while (failed)
  147         {
  148             /* while */
  149             tst = tst + 1;
  150             DEBUG printf("testing %d\n", tst);
  151             init(t);
  152             DEBUG dump(t);
  153             failed = ! testIt(tst, t);
  154             DEBUG dump(t);
  155         } /* while */
  156     printf("%d\n", tst);
  157 } /* FUNCTION process */
  158 
  159 int main()
  160 {
  161     /* main */
  162     int moreToDo;
  163 
  164     moreToDo = getInput();
  165     while (moreToDo)
  166         {
  167             /* while */
  168             process();
  169             moreToDo = getInput();
  170         } /* while */
  171 
  172     return EXIT_SUCCESS;
  173 } /* main */
  174