Computer Programming Contest Preparation

ToolBox - Source for: 3/324/c.c



/home/toolbox/public_html/solutions/3/324/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 <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 #define killedGoodGuy 9999999
   29 
   30 int k;
   31 
   32 int getInput()
   33 {
   34     /* FUNCTION getInput */
   35     int dataReadFlag;
   36     scanf(" %d ", &k);
   37     dataReadFlag = 0 < k;
   38     return (dataReadFlag);
   39 } /* FUNCTION getInput */
   40 
   41 void process()
   42 {
   43     /* FUNCTION process */
   44     int n;
   45     int i;
   46     int notFound = TRUE;
   47     int m;
   48     int pos;
   49 
   50     /* pos = prevPos + m - 1) % (n -i + 1) */
   51     n = k * 2;
   52     m = k;
   53     while (notFound)
   54         {
   55             /* while */
   56             m = m + 1;
   57             DEBUG printf("%d: trying %d\n", k, m);
   58             pos = 0;
   59             for (i=0; i<k; i++)
   60                 {
   61                     /* try up to k kills */
   62                     pos = (pos + m - 1) % (n - i);
   63                     DEBUG printf("pos: %d   i: %d\n", pos, i);
   64                     if (pos < k)
   65                         {
   66                             i = killedGoodGuy;
   67                         }
   68                 } /* try up to k kills */
   69             notFound = (i != k);
   70         } /* while */
   71     DEBUG printf("answer for %d is ", k);
   72     printf("%d\n", m);
   73 } /* FUNCTION process */
   74 
   75 int main()
   76 {
   77     /* main */
   78     int moreToDo;
   79 
   80     moreToDo = getInput();
   81     while (moreToDo)
   82         {
   83             /* while */
   84             process();
   85             moreToDo = getInput();
   86         } /* while */
   87 
   88     return EXIT_SUCCESS;
   89 } /* main */
   90