Computer Programming Contest Preparation

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



/home/toolbox/public_html/solutions/2/256/b.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 
   29 int num;
   30 int squares[MAX];
   31 int sqCnt = 0;
   32 int tens[4] = {1, 10, 100, 1000};
   33 
   34 int check(int test)
   35 {
   36     /* FUNCTION check */
   37     int p1;
   38     int p2;
   39     int sq;
   40     int p;
   41     int idx;
   42 
   43     sq = test * test;
   44     idx = 0;
   45     while (tens[idx] <= test)
   46         {
   47             idx++;    /* figure out how many digits */
   48         }
   49     p1 = sq / tens[idx];
   50     p2 = sq % tens[idx];
   51     p = p1 + p2;
   52     DEBUG printf("tens[%d] = %d\n", idx, tens[idx]);
   53     DEBUG printf ("sq: %d  p1: %d  p2: %d  p: %d   p*p: %d\n", sq, p1, p2, p, p*p);
   54     return ((p * p) == sq);
   55 } /* FUNCTION check */
   56 
   57 void init()
   58 {
   59     /* FUNCTION init */
   60     int i;
   61     int mn;
   62     int mx;
   63     int tmp;
   64 
   65     /* 2 digit */
   66     mn = 9;
   67     mx = 100000000;
   68     for (i=0; (i*i)<mx; i++)
   69         {
   70             /* for  */
   71             if (check(i))
   72                 {
   73                     /* keep this one */
   74                     DEBUG printf("squares[%d] = %d\n", sqCnt, i*i);
   75                     squares[sqCnt++] = i*i;
   76                 } /* keep this one */
   77         } /* for  */
   78 } /* FUNCTION init */
   79 
   80 void dump()
   81 {
   82     /* FUNCTION dump */
   83 } /* FUNCTION dump */
   84 
   85 int getInput()
   86 {
   87     /* FUNCTION getInput */
   88     int dataReadFlag;
   89 
   90     dataReadFlag = (1 == scanf(" %d ", &num));
   91     return (dataReadFlag);
   92 } /* FUNCTION getInput */
   93 
   94 void process()
   95 {
   96     /* FUNCTION process */
   97     int i;
   98     int digits[9] = { 0, 0, 100, 0, 10000, 0, 1000000, 0, 100000000};
   99 
  100     for (i=0; i<sqCnt; i++)
  101         {
  102             /* for */
  103             if (squares[i] < digits[num])
  104                 {
  105                     /* found a square small enough */
  106                     printf("%0*d\n", num, squares[i]);
  107                 } /* found a square small enough */
  108         } /* for */
  109     printf("\n");
  110 } /* FUNCTION process */
  111 
  112 int main()
  113 {
  114     /* main */
  115     int moreToDo;
  116 
  117     init();
  118     moreToDo = getInput();
  119     while (moreToDo)
  120         {
  121             /* while */
  122             process();
  123             moreToDo = getInput();
  124         } /* while */
  125 
  126     return EXIT_SUCCESS;
  127 } /* main */
  128