AlgorithmQuiz/3Plus1Cee로 --destine

   1 #include "stdafx.h"
   2 #include <stdlib.h>
   3 
   4 int getCycleLength( int k )
   5 {
   6         int nCycle = 1;
   7 
   8         while( k != 1 )
   9         {
  10                 if ( k%2 == 0 )
  11                 {
  12                         k = k / 2;
  13                 }
  14                 else
  15                 {
  16                         k = k * 3 + 1;
  17                 }
  18 
  19                 nCycle++;
  20         }
  21 
  22         return nCycle;
  23 }
  24 
  25 int getMaxCycleLength( int i, int j )
  26 {
  27         int nMax = 0;
  28         for ( int k = i; k < j; k++ )
  29         {
  30                 int nTemp = getCycleLength( k );
  31 
  32                 if ( nMax < nTemp )
  33                         nMax = nTemp;
  34         }
  35         return nMax;
  36 }
  37 
  38 
  39 int main(int argc, char* argv[])
  40 {
  41         if ( argc != 3 )
  42         {
  43                 printf( "3nplus1  숫자  숫자\n" );
  44                 return 0;
  45         }
  46 
  47         int i = atoi(argv[1]);
  48         int j = atoi(argv[2]);
  49 
  50         if ( i > j )
  51                 return 0;
  52 
  53         int nMax = getMaxCycleLength(i, j);
  54 
  55         printf("%d %d %d", i,j,nMax);
  56         return 0;
  57 }

AlgorithmQuiz/3Plus1/destine (last edited 2013-08-19 14:53:26 by 61)

web biohackers.net