Computer Programming Contest Preparation

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



/home/toolbox/public_html/solutions/3/305/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: 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 17
   27 #define MAXX 30
   28 #define killedGoodGuy 9999999
   29 
   30 int k;
   31 int ans[MAX_NUM];
   32 
   33 void init()
   34 {
   35     /* FUNCTION init */
   36     int k;
   37     int m;
   38     int n;
   39     int pos;
   40     int i;
   41     int notFound;
   42 
   43     for (k=1; k<MAX_NUM; k++)
   44         {
   45             /* for each possible k */
   46             /* pos = prevPos + m - 1) % (n - i) */
   47             /* position (where I am now)
   48              * + m      (how many folks to skip)
   49              * - 1      (subtract 1 to make 1-n become 0 to n-1)
   50              * n        (total number of people to start with)
   51              * - i      (number of people already eliminated)
   52              */
   53             n = k * 2;
   54             m = k;
   55             notFound = TRUE;
   56             while (notFound)
   57                 {
   58                     /* while */
   59                     m = m + 1;
   60                     pos = 0;
   61                     for (i=0; i<k; i++)
   62                         {
   63                             /* try up to k kills */
   64                             pos = (pos + m - 1) % (n - i);
   65                             if (pos < k)
   66                                 {
   67                                     i = killedGoodGuy;
   68                                 }
   69                         } /* try up to k kills */
   70                     notFound = (i != k);
   71                 } /* while */
   72             ans[k] = m;
   73         } /* for each possible k */
   74 } /* FUNCTION init */
   75 
   76 int getInput()
   77 {
   78     /* FUNCTION getInput */
   79     int dataReadFlag;
   80     scanf(" %d ", &k);
   81     dataReadFlag = 0 < k;
   82     return (dataReadFlag);
   83 } /* FUNCTION getInput */
   84 
   85 void process()
   86 {
   87     /* FUNCTION process */
   88 
   89     printf("%d\n", ans[k]);
   90 } /* FUNCTION process */
   91 
   92 int main()
   93 {
   94     /* main */
   95     int moreToDo;
   96 
   97     init();
   98     moreToDo = getInput();
   99     while (moreToDo)
  100         {
  101             /* while */
  102             process();
  103             moreToDo = getInput();
  104         } /* while */
  105 
  106     return EXIT_SUCCESS;
  107 } /* main */
  108