Computer Programming Contest Preparation

ToolBox - Source for: 6/636/j.c



/home/toolbox/public_html/solutions/6/636/j.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 #define BIG unsigned long long int
   16 #define STOPBASE 100
   17 
   18 /*
   19  *  Author: Isaac Traxler
   20  *    Date: 2013-02-08
   21  * Purpose: fun
   22  * Problem: 636 Squares
   23  */
   24 
   25 /*
   26  * This template reads data until a terminating value is reached.
   27  */
   28 
   29 BIG inp;
   30 BIG num;
   31 
   32 
   33 void init()
   34 {
   35     /* FUNCTION init */
   36 } /* FUNCTION init */
   37 
   38 void dump()
   39 {
   40     /* FUNCTION dump */
   41 } /* FUNCTION dump */
   42 
   43 int getInput()
   44 {
   45     /* FUNCTION getInput */
   46     int dataReadFlag;
   47 
   48     scanf(" %lld ", &inp);
   49     dataReadFlag = 0 != inp;
   50     return (dataReadFlag);
   51 } /* FUNCTION getInput */
   52 
   53 int scanDigit(BIG inp)
   54 {
   55     /* FUNCTION scanDigit */
   56     BIG tmp;
   57     int dig = 1;
   58     int td;
   59 
   60     tmp = inp;
   61     while ((dig < 9) && (0 < tmp))
   62         {
   63             /* while */
   64             td = tmp % 10;
   65             tmp = tmp / 10;
   66             if (td > dig)
   67                 {
   68                     dig = td;
   69                 }
   70         } /* while */
   71     return dig;
   72 } /* FUNCTION scanDigit */
   73 
   74 BIG cnvrt(BIG inp, int base)
   75 {
   76     /* FUNCTION cnvrt */
   77     BIG tmp;
   78     BIG tot = 0;
   79     BIG mult = 1;
   80 
   81     tmp = inp;
   82     while (0 < tmp)
   83         {
   84             /* while */
   85             tot = tot + (mult * (tmp % 10));
   86             tmp = tmp / 10;
   87             mult = mult * base;
   88         } /* while */
   89     return tot;
   90 } /* FUNCTION cnvrt */
   91 
   92 int isNotSquare(BIG num)
   93 {
   94     /* FUNCTION isNotSquare */
   95     BIG tmp;
   96     int notSquare;
   97 
   98     tmp = (BIG) sqrt((double) num);
   99     DEBUG printf("%lld -> %lld\n", num, tmp);
  100     notSquare = ((tmp * tmp) != num);
  101     return notSquare;
  102 } /* FUNCTION isNotSquare */
  103 
  104 void process()
  105 {
  106     /* FUNCTION process */
  107     int i;
  108     int notFound = TRUE;
  109     int startBase;
  110 
  111     /* search for the base */
  112     /* determine min base */
  113     startBase = scanDigit(inp) + 1;
  114     DEBUG printf("start=%d\n", startBase);
  115     for (i=startBase; (STOPBASE > i) && (notFound); i++)
  116         {
  117             /* for each possible base */
  118             num = cnvrt(inp, i);
  119             notFound = isNotSquare(num);
  120             DEBUG printf("%lld (%d)  is %lld (10)\n", inp, i, num);
  121         } /* for each possible base */
  122     DEBUG printf("%lld\n", inp);
  123     printf("%d\n", i - 1);
  124 } /* FUNCTION process */
  125 
  126 int main ()
  127 {
  128     /* main */
  129     int moreToDo;
  130 
  131     init();
  132     moreToDo = getInput();
  133     while (moreToDo)
  134         {
  135             /* while */
  136             process();
  137             moreToDo = getInput();
  138         } /* while */
  139 
  140     return EXIT_SUCCESS;
  141 } /* main */
  142