Computer Programming Contest Preparation

ToolBox - Source for: 2/256/a.c



/home/toolbox/public_html/solutions/2/256/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 /*
   17  *  Author: Isaac Traxler
   18  *    Date: 2015-03-23
   19  * Purpose:
   20  * Problem: 256
   21  */
   22 
   23 /*
   24  * This template reads lines of data at a time until end of file.
   25  */
   26 
   27 #define MAX 25
   28 #define DIGITS 9
   29 
   30 int num;
   31 int squares[MAX][DIGITS];
   32 int counts[DIGITS];
   33 
   34 int mn[DIGITS]   = {0, 0,   2, 0,    10, 0,     100, 0,      1000};
   35 int tens[DIGITS] = {0, 0, 100, 0, 10000, 0, 1000000, 0, 100000000};
   36 int divs[DIGITS] = {0, 0,  10, 0,   100, 0,    1000, 0,     10000};
   37 
   38 int check(int test, int idx)
   39 {
   40     /* FUNCTION check */
   41     int p1;
   42     int p2;
   43     int sq;
   44     int p;
   45 
   46     sq = test * test;
   47     p1 = sq / divs[idx];
   48     p2 = sq % divs[idx];
   49     p = p1 + p2;
   50     DEBUG printf("divs[%d] = %d\n", idx, divs[idx]);
   51     DEBUG printf ("sq: %d  p1: %d  p2: %d  p: %d   p*p: %d\n", sq, p1, p2, p, p*p);
   52     return ((p * p) == sq);
   53 } /* FUNCTION check */
   54 
   55 void init()
   56 {
   57     /* FUNCTION init */
   58     int i;
   59     int j;
   60 
   61     for (i=2; i<DIGITS; i=i+2)
   62         {
   63             /* for  */
   64             squares[0][i] = 0;
   65             squares[1][i] = 1;
   66             counts[i] = 2;
   67             DEBUG printf("for (j=mn[i<%d>]<%d>; j<divs[i]<%d>; j++)\n", i, mn[i], divs[i]);
   68             for (j=mn[i]; j<divs[i]; j++)
   69                 {
   70                     /* for */
   71                     if (check(j, i))
   72                         {
   73                             /* keep this one */
   74                             DEBUG printf("squares[%d][%d] = %d\n", counts[i], i, j*j);
   75                             squares[counts[i]][i] = j * j;
   76                             counts[i] = counts[i] + 1;
   77                         } /* keep this one */
   78                 } /* for */
   79         } /* for  */
   80 } /* FUNCTION init */
   81 
   82 void dump()
   83 {
   84     /* FUNCTION dump */
   85 } /* FUNCTION dump */
   86 
   87 int getInput()
   88 {
   89     /* FUNCTION getInput */
   90     int dataReadFlag;
   91 
   92     dataReadFlag = (1 == scanf(" %d ", &num));
   93     return (dataReadFlag);
   94 } /* FUNCTION getInput */
   95 
   96 void process()
   97 {
   98     /* FUNCTION process */
   99     int i;
  100 
  101     for (i=0; i<counts[num]; i++)
  102         {
  103             /* for */
  104             if (squares[i][num] < tens[num])
  105                 {
  106                     /* found a square small enough */
  107                     printf("%0*d\n", num, squares[i][num]);
  108                 } /* found a square small enough */
  109         } /* for */
  110     DEBUG printf("\n");
  111 } /* FUNCTION process */
  112 
  113 int main()
  114 {
  115     /* main */
  116     int moreToDo;
  117 
  118     init();
  119     moreToDo = getInput();
  120     while (moreToDo)
  121         {
  122             /* while */
  123             process();
  124             moreToDo = getInput();
  125         } /* while */
  126 
  127     return EXIT_SUCCESS;
  128 } /* main */
  129