Computer Programming Contest Preparation

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



/home/toolbox/public_html/solutions/6/636/s3.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 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(" %ld ", &inp);
   49     dataReadFlag = 0 != inp;
   50     return (dataReadFlag);
   51 } /* FUNCTION getInput */
   52 
   53 int scanDigit()
   54 {
   55     /* FUNCTION scanDigit */
   56     BIG tmp;
   57     int dig = 1;
   58     int td;
   59 
   60     tmp = inp;
   61     while ((td < 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("%ld -> %ld\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     /* search for the base */
  111     /* determine min base */
  112     startBase = scanDigit() + 1;
  113     for (i=startBase; (STOPBASE > i) && (notFound); i++)
  114         {
  115             /* for each possible base */
  116             num = cnvrt(inp, i);
  117             notFound = isNotSquare(num);
  118             printf("%ld (%d)  is %ld (10)\n", inp, i, num);
  119         } /* for each possible base */
  120     printf("%ld\n", inp);
  121 } /* FUNCTION process */
  122 
  123 int main ()
  124 {
  125     /* main */
  126     int moreToDo;
  127 
  128     init();
  129     moreToDo = getInput();
  130     while (moreToDo)
  131         {
  132             /* while */
  133             process();
  134             moreToDo = getInput();
  135         } /* while */
  136 
  137     return EXIT_SUCCESS;
  138 } /* main */
  139