Computer Programming Contest Preparation

ToolBox - Source for: 1/100/judged.c



/home/toolbox/public_html/solutions/1/100/judged.c
    1 /*
    2    add array to optimize
    3 */
    4 
    5 #include <stdio.h>
    6 
    7 #define TRUE  (1 == 1)
    8 #define FALSE (1 != 1)
    9 #define MININT (-999999)
   10 #define MAXSIZE 1000000
   11 
   12 int ary[2*MAXSIZE] = {0};
   13 
   14 void dump()
   15 {
   16     /* BEGIN FUNCTION dump */
   17     int i;
   18 
   19     printf("\n");
   20     for (i=1; i < MAXSIZE; i++)
   21         {
   22             /* for */
   23             if (0 != ary[i])
   24                 {
   25                     /* if */
   26                     printf("ary[%d] = (%d)\n", i, ary[i]);
   27                 } /* if */
   28         } /* for */
   29 } /* BEGIN FUNCTION dump */
   30 
   31 int getInput(int *start, int *stop, int *s1, int *s2)
   32 {
   33     /* BEGIN FUNCTION getInput */
   34     int moreToDo;
   35     int tmp;
   36 
   37     moreToDo = EOF != scanf("%d ", start);
   38     /*    printf("moreToDo=[%d]\n",moreToDo); */
   39     if (moreToDo)
   40         {
   41             /* then */
   42             scanf("%d ", stop);
   43             if (*start > *stop)
   44                 {
   45                     /* then */
   46                     *s1 = *start;
   47                     *s2 = *stop;
   48                 } /* then */
   49             else
   50                 {
   51                     /* else */
   52                     *s1 = *stop;
   53                     *s2 = *start;
   54                 } /* else */
   55         } /* then */
   56     return moreToDo;
   57 } /* END FUNCTION getInput */
   58 
   59 unsigned long int calcOne(unsigned long int in)
   60 {
   61     /* BEGIN FUNCTION calcOne */
   62     unsigned long int retval = 1;
   63     if (1 != in)
   64         {
   65             /* then */
   66             retval = (0 == (in % 2)) ? in / 2 : 3 * in + 1 ;
   67         } /* then */
   68     return retval;
   69 } /* END FUNCTION calcOne */
   70 
   71 unsigned long int calc(unsigned long int in)
   72 {
   73     /* BEGIN FUNCTION calc */
   74     unsigned long int retval;
   75     int cnt = 1;
   76     unsigned long int tmp;
   77     unsigned long int t1;
   78     int xx = 0;
   79 
   80     /*    printf("Enter calc (%d)\n", in);     */
   81     /*       if (0 == (tot % 10000))           */
   82     /*        {                                */
   83     /*           tot = 0;                      */
   84     /*           tot1++;                       */
   85     /*           printf("tot1=(%ld)\n", tot1); */
   86     /*        }                                */
   87     /*       tot++;                            */
   88     retval = in;
   89     while ((1 != retval) && (retval > 0))
   90         {
   91             /* while */
   92             if (MAXSIZE > retval)
   93                 {
   94                     /* then */
   95                     if (0 != ary[retval])
   96                         {
   97                             /* then */
   98                             cnt = cnt + ary[retval] - 1;
   99                         } /* then */
  100                     else
  101                         {
  102                             /* else */
  103                             tmp = calcOne(retval);
  104                             t1 = calc(tmp);
  105                             cnt = cnt + t1;
  106                             ary[retval] = cnt - xx;
  107                             /*               printf("Array updates: ary[%d] = (%d)\n", retval, ary[retval]); */
  108                         } /* else */
  109                     /*       printf("retval=(%d) cnt=(%d)\n", retval, cnt); */
  110                     retval = 1;
  111                 } /* then */
  112             else
  113                 {
  114                     /* else */
  115                     retval = calcOne(retval);
  116                     cnt++;
  117                     xx++;
  118                     /*       printf("retval=(%d) cnt=(%d)\n", retval, cnt); */
  119                 } /* else */
  120         } /* while */
  121     return (cnt);
  122 } /* BEGIN FUNCTION calc */
  123 
  124 int process(int start, int stop)
  125 {
  126     /* BEGIN FUNCTION process */
  127     int i;
  128     int max = MININT;
  129     int tmp;
  130 
  131     for (i=start; i >= stop; i--)
  132         {
  133             /* for */
  134             tmp = calc(i);
  135             max = (max < tmp) ? tmp : max;
  136         } /* for */
  137 
  138     return max;
  139 } /* END FUNCTION process */
  140 
  141 int main ()
  142 {
  143     /* main */
  144     int moreToDo;  /* tell me when to stop */
  145     int start;     /* starting point */
  146     int stop;      /* stopping point */
  147     int s1;
  148     int s2;
  149     unsigned long int ans;       /* calculated answer */
  150 
  151     /*    init(); */
  152     moreToDo = getInput(&start, &stop, &s1, &s2);
  153     while (moreToDo)
  154         {
  155             /* while */
  156             ans = process(s1, s2);
  157             /*        dump(); */
  158             printf("%d %d %ld\n", start, stop, ans);
  159             moreToDo = getInput(&start, &stop, &s1, &s2);
  160         } /* while */
  161 
  162     return 0;
  163 } /* main */
  164