Computer Programming Contest Preparation

ToolBox - Source for: 3/386/a.c



/home/toolbox/public_html/solutions/3/386/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:
   18  * Purpose: fun
   19  * Problem:
   20  */
   21 
   22 /*
   23  * This template reads data until a terminating value is reached.
   24  */
   25 
   26 #define MAX 202
   27 int cubes[MAX];
   28 
   29 void init()
   30 {
   31     /* FUNCTION init */
   32     int i;
   33 
   34     for (i=1; i<MAX; i++)
   35         {
   36             /* cache cubes */
   37             cubes[i] = i * i * i;
   38         } /* cache cubes */
   39 } /* FUNCTION init */
   40 
   41 void dump()
   42 {
   43     /* FUNCTION dump */
   44 } /* FUNCTION dump */
   45 
   46 void process()
   47 {
   48     /* FUNCTION process */
   49     int a;
   50     int b;
   51     int c;
   52     int d;
   53     int left;
   54     int leftB;
   55 
   56     for (a=6; 200>=a; a++)
   57         {
   58             /* check each cube to see if it has soultions */
   59             for (b=2; 200>b; b++)
   60                 {
   61                     /* try each possible b */
   62                     left = cubes[a] - cubes[b];
   63                     leftB = left;
   64                     for (c=(b+1); ((200>c) && (0<leftB)); c++)
   65                         {
   66                             /* now try all Cs */
   67                             leftB = left - cubes[c];
   68                             for (d=(c+1); ((200>d) && (cubes[d]<leftB)); d++) ; /* try Ds until a match is found or impossible */
   69                             if (leftB == cubes[d])
   70                                 {
   71                                     /* solution found */
   72                                     printf("Cube = %d, Triple = (%d,%d,%d)\n", a, b, c, d);
   73                                 } /* solution found */
   74                         } /* now try all Cs */
   75                 } /* try each possible b */
   76         } /* check each cube to see if it has soultions */
   77 } /* FUNCTION process */
   78 
   79 int main()
   80 {
   81     /* main */
   82     int moreToDo;
   83 
   84     init();
   85     process();
   86 
   87     return EXIT_SUCCESS;
   88 } /* main */
   89