Computer Programming Contest Preparation

ToolBox - Source for: 120/12027/a.c



/home/toolbox/public_html/solutions/120/12027/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-02-02
   18  * Purpose: fun
   19  * Problem: 12027 - Very Big Perfect Squares
   20 
   21 If you were to take the square root of the number (integer part), you would know
   22 the largest number that could be squared and be less than number read in. This
   23 number would be the number of perfect squares less than the read in number. If
   24 the number read in is 65 -- the square root is 8. The perfect squares less than
   25 65 are 1^2, 2&2, ... 7^2, 8^2. So the answer is 8.
   26 
   27 This is not practical for really large values.
   28 
   29 Since we need only get the answer to 1 significant value, we can cheat:
   30 
   31 - numbers with odd number of digits
   32 -- take square root of first digit and add 1 0 after it for each pair of remaining digits
   33 
   34 - numbers with even number of digits
   35 -- take square root of first 2 digits and add 1 0 for each remaining pair of digits
   36 
   37  */
   38 
   39 /*
   40  * This template reads data until a terminating value is reached.
   41  */
   42 
   43 #define MAX_LENGTH 1005
   44 
   45 char buff[MAX_LENGTH];
   46 int lngth;
   47 
   48 void init()
   49 {
   50     /* FUNCTION init */
   51 } /* FUNCTION init */
   52 
   53 void dump()
   54 {
   55     /* FUNCTION dump */
   56 } /* FUNCTION dump */
   57 
   58 int getInput()
   59 {
   60     /* FUNCTION getInput */
   61     int dataReadFlag;
   62 
   63     scanf(" %s ", buff);
   64 
   65     lngth = strlen(buff);
   66 
   67     dataReadFlag = ! ((1 == lngth) && ('0' == buff[0]));
   68     return (dataReadFlag);
   69 } /* FUNCTION getInput */
   70 
   71 void process()
   72 {
   73     /* FUNCTION process */
   74     int tmp;
   75     int i;
   76     int cnt;
   77 
   78     DEBUG printf("%d [%s]\n", lngth, buff);
   79 
   80     /* how many zeroes */
   81     cnt = (lngth - 1) / 2;
   82     /* convert first character to a digit */
   83     tmp = buff[0] - '0';
   84     if (0 == (lngth % 2))
   85         {
   86             /* even number of digits */  /* account for second digit on even number of digits */
   87             tmp = (tmp * 10) + (buff[1] - '0');
   88         } /* even number of digits */
   89 
   90     tmp = sqrt(tmp+0.0);
   91     printf("%d", tmp);
   92     for (i=0; i<cnt; i++)
   93         {
   94             printf("0");
   95         }
   96     printf("\n");
   97 } /* FUNCTION process */
   98 
   99 int main()
  100 {
  101     /* main */
  102     int moreToDo;
  103 
  104     init();
  105     moreToDo = getInput();
  106     while (moreToDo)
  107         {
  108             /* while */
  109             process();
  110             moreToDo = getInput();
  111         } /* while */
  112 
  113     return EXIT_SUCCESS;
  114 } /* main */
  115