Computer Programming Contest Preparation

ToolBox - Source for: 102/10276/a.c



/home/toolbox/public_html/solutions/102/10276/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 <stdlib.h>
    7 #include <math.h>
    8 #include <stdint.h>
    9 #include <ctype.h>
   10 
   11 #define TRUE  (1 == 1)
   12 #define FALSE (1 != 1)
   13 
   14 #define DEBUG if (FALSE)
   15 
   16 /* fprintf(stderr, "functionName: message", varslist); */
   17 
   18 /*
   19  *  Author: Isaac Traxler
   20  *    Date: 2024-11-05
   21  * Purpose: fun
   22  * Problem: 10276 - Hanoi Tower Troubles Again!
   23  */
   24 
   25 /*
   26  * This template reads data a specified number of times.
   27  */
   28 
   29 #define MAX_PEGS  52
   30 
   31 int numberOfTimes;
   32 int pegs;
   33 int p[MAX_PEGS];
   34 int ans[MAX_PEGS];
   35 
   36 void init()
   37 {
   38     /* FUNCTION init */
   39     int i;
   40     int j;
   41     int cnt;
   42     int pCnt;
   43     int accepted;
   44     int t1;
   45     int t2;
   46 
   47     scanf("%d ", &numberOfTimes);
   48 
   49     p[0] = 0;
   50     p[1] = 1;
   51     ans[0] = 0;
   52     cnt = 2;   /* ball number currently on */
   53     pCnt = 1;
   54     for (i=2; MAX_PEGS>pCnt; )
   55         {
   56             /* for each additional peg */
   57             DEBUG printf("(i %d)\n", i);
   58             accepted = FALSE;
   59             for (j=1; (! accepted) && (i>j); j++)
   60                 {
   61                     /* try each peg */
   62                     t1 = p[j] + cnt;
   63                     t2 = sqrt(t1);
   64                     if ((t2 * t2) == t1)
   65                         {
   66                             /* found where ball can go */
   67                             accepted = TRUE;
   68                             DEBUG printf("p[%d] = %d\n", j, cnt);
   69                             p[j] = cnt;
   70                             cnt++;
   71                         } /* found where ball can go */
   72                 } /* try each peg */
   73             if (! accepted)
   74                 {
   75                     /* new peg time */
   76                     DEBUG printf("%d: Adding peg: p[%d] = %d,  ans[%d] = %d\n", i, pCnt, cnt, pCnt-1, cnt - 1);
   77                     ans[pCnt] = cnt - 1;
   78                     pCnt++;
   79                     p[pCnt] = cnt;
   80                     cnt++;
   81                     i++;
   82                 } /* new peg time */
   83         } /* for each additional peg */
   84 } /* FUNCTION init */
   85 
   86 void dump()
   87 {
   88     /* FUNCTION dump */
   89 } /* FUNCTION dump */
   90 
   91 void getInput()
   92 {
   93     /* FUNCTION getInput */
   94     scanf(" %d ", &pegs);
   95 } /* FUNCTION getInput */
   96 
   97 void process()
   98 {
   99     /* FUNCTION process */
  100     printf("%d\n", ans[pegs]);
  101 } /* FUNCTION process */
  102 
  103 int main()
  104 {
  105     /* main */
  106     int i;
  107 
  108     init();
  109     for (i=0; i<numberOfTimes; i++)
  110         {
  111             /* while */
  112             getInput();
  113             process();
  114         } /* while */
  115 
  116     return EXIT_SUCCESS;
  117 } /* main */
  118 
  119