Computer Programming Contest Preparation

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



/home/toolbox/public_html/solutions/1/100/simple1.c
    1 //
    2 // take into accouint that pairs are not necessarily in order
    3 //
    4 
    5 #include <stdio.h>
    6 
    7 #define TRUE  (1 == 1)
    8 #define FALSE (1 != 1)
    9 #define MININT (-999999)
   10 
   11 
   12 int getInput(int *start, int *stop, int *s1, int *s2)
   13 {
   14     // BEGIN FUNCTION getInput
   15     int moreToDo;
   16     int tmp;
   17 
   18     moreToDo = EOF != scanf("%d ", start);
   19 //    printf("moreToDo=[%d]\n",moreToDo);
   20     if (moreToDo)
   21         {
   22             // then
   23             scanf("%d ", stop);
   24             if (*start < *stop)
   25                 {
   26                     // then
   27                     *s1 = *stop;
   28                     *s2 = *start;
   29                 } // then
   30             else
   31                 {
   32                     // else
   33                     *s1 = *start;
   34                     *s2 = *stop;
   35                 } // else
   36         } // then
   37     return moreToDo;
   38 } // END FUNCTION getInput
   39 
   40 int calcOne(int in)
   41 {
   42     // BEGIN FUNCTION calcOne
   43     int retval = 1;
   44     if (1 != in)
   45         {
   46             // then
   47             retval = (0 == (in % 2)) ? in / 2 : 3 * in + 1 ;
   48         } // then
   49     return retval;
   50 } // END FUNCTION calcOne
   51 
   52 int calc(int in)
   53 {
   54     // BEGIN FUNCTION calc
   55     int retval;
   56     int cnt = 1;
   57 
   58     retval = in;
   59     while (1 != retval)
   60         {
   61             // while
   62             cnt++;
   63             retval = calcOne(retval);
   64         } // while
   65     return cnt;
   66 } // BEGIN FUNCTION calc
   67 
   68 int process(int start, int stop)
   69 {
   70     // BEGIN FUNCTION process
   71     int i;
   72     int max = MININT;
   73     int tmp;
   74 
   75     for (i=start; i >= stop; i--)
   76         {
   77             // for
   78             tmp = calc(i);
   79             max = (max < tmp) ? tmp : max;
   80         } // for
   81 
   82     return max;
   83 } // END FUNCTION process
   84 
   85 int main ()
   86 {
   87     // main
   88     int moreToDo;  // tell me when to stop
   89     int start;     // starting point
   90     int stop;      // stopping point
   91     int s1;
   92     int s2;
   93     int ans;       // calculated answer
   94 
   95 //    init();
   96     moreToDo = getInput(&start, &stop, &s1, &s2);
   97     while (moreToDo)
   98         {
   99             // while
  100             ans = process(s1, s2);
  101             printf("%d %d %d\n", start, stop, ans);
  102             moreToDo = getInput(&start, &stop, &s1, &s2);
  103         } // while
  104 
  105     return 1;
  106 } // main
  107