Computer Programming Contest Preparation

ToolBox - Source for: 109/10920/a.c



/home/toolbox/public_html/solutions/109/10920/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: 2016-08-24
   18  * Purpose: fun
   19  * Problem: 10820 - spinal tap
   20  */
   21 
   22 /*
   23  * This template reads data until a terminating value is reached.
   24  */
   25 
   26 #define NUMSIZE long long int
   27 
   28 int dim;
   29 NUMSIZE goal;
   30 
   31 void init()
   32 {
   33     /* FUNCTION init */
   34 } /* FUNCTION init */
   35 
   36 void dump()
   37 {
   38     /* FUNCTION dump */
   39 } /* FUNCTION dump */
   40 
   41 int getInput()
   42 {
   43     /* FUNCTION getInput */
   44     int dataReadFlag;
   45 
   46     scanf(" %d %lld ", &dim, &goal);
   47     dataReadFlag = 0 != dim;
   48     return (dataReadFlag);
   49 } /* FUNCTION getInput */
   50 
   51 void process()
   52 {
   53     /* FUNCTION process */
   54     NUMSIZE tmp;
   55     int cntr;
   56     int row;
   57     int col;
   58     NUMSIZE squp; /* first square of odd number greater than goal */
   59     NUMSIZE sqdn; /* first square of odd number less than goal */
   60     int sqrup;    /* square root of upper square */
   61     int sqrdn;    /* square root of lower square */
   62     int diff;
   63     int edgeLength;
   64 
   65     tmp = sqrt(goal);
   66     cntr = (dim + 1) / 2;
   67     /* is goal a perfect square of an odd number */
   68     if ((1 == (tmp % 2)) && ((tmp * tmp) == goal))
   69         {
   70             /* perfect odd square */
   71             col = cntr + (tmp / 2);
   72             row = col;
   73         } /* perfect odd square */
   74     else
   75         {
   76             /* so much for the easy case */
   77             if (1 == (tmp % 2))
   78                 {
   79                     /* odd */
   80                     sqrdn = tmp;
   81                 } /* odd */
   82             else
   83                 {
   84                     /* even */
   85                     sqrdn = tmp - 1;
   86                 } /* even */
   87             sqrup = sqrdn + 2;
   88             row = cntr + (sqrup / 2);
   89             col = row;
   90             squp = ((NUMSIZE) sqrup) * sqrup;
   91             sqdn = ((NUMSIZE) sqrdn) * sqrdn;
   92             edgeLength = (squp - sqdn) / 4; /* how many numbers are along an edge -1 */
   93             diff = squp - goal;  /* how many squares back */
   94 
   95             /*      33330  dividing diff by 4 determines if square is in section
   96              *      2   0  0, 1, 2, or 3. This tells us how much and how to tweak row
   97              *      2   0  and column.
   98              *      2   0
   99              *      21111
  100              */
  101             DEBUG printf(" dim = %d\n", dim);
  102             DEBUG printf(" goal = %lld\n", goal);
  103             DEBUG printf(" tmp = %lld\n", tmp);
  104             DEBUG printf(" row = %d\n", row);
  105             DEBUG printf(" col = %d\n", col);
  106             DEBUG printf(" cntr = %d\n", cntr);
  107             DEBUG printf(" sqrdn = %d\n", sqrdn);
  108             DEBUG printf(" sqrup = %d\n", sqrup);
  109             DEBUG printf(" edgeLength = %d\n", edgeLength);
  110             DEBUG printf(" diff = %d\n", diff);
  111             switch (diff / edgeLength)
  112                 {
  113                 /* 4 cases */
  114                 case 0: /* goal is in 0 section */
  115                     row = row - diff;
  116                     break;
  117                 case 1: /* goal is in 1 section */
  118                     row = row - edgeLength;
  119                     col = col + edgeLength - diff;
  120                     break;
  121                 case 2: /* goal is in 2 section */
  122                     row = row + diff - (3 * edgeLength);
  123                     col = col - edgeLength;
  124                     break;
  125                 case 3: /* goal is in 3 section */
  126                     col = col + diff - (4 * edgeLength);
  127                     break;
  128                 } /* 4 cases */
  129 
  130 
  131 
  132         } /* so much for the easy case */
  133     printf("Line = %d, column = %d.\n", row, col);
  134 } /* FUNCTION process */
  135 
  136 int main()
  137 {
  138     /* main */
  139     int moreToDo;
  140 
  141     init();
  142     moreToDo = getInput();
  143     while (moreToDo)
  144         {
  145             /* while */
  146             process();
  147             moreToDo = getInput();
  148         } /* while */
  149 
  150     return EXIT_SUCCESS;
  151 } /* main */
  152