3n+1 Problem

Algorithm/Acm 2008/02/17 08:30 귀차니스트
  1. 문제링크 : http://icpcres.ecs.baylor.edu/onlinejudge/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=36
  2.  의   경   : 처음으로 나오는 쉬운 기초적인 문제입니다. 하지만 체크해야될 조건이 있습니다. 바로 입력 숫자의 크기가 서로 바뀔 수 있다는 점이죠.
    입력 방식이 10 20과 같이 입력될 때에는 괜찮으나 20 10과 같이 입력될 때에는 체크를 꼭 해주어야 합니다.
  3. 소스
    100.c (Language : c)
    1. #include <stdio.h>
    2. int Calculation( int number );
    3. int main( int argc, char **argv )
    4. {
    5.     int StInput, EnInput;
    6.     int StartNum, EndNum, RetValue;
    7.     int i, MaxCycle;
    8.     while( scanf( "%d %d", &StInput, &EnInput ) == 2 ) {
    9.         if( StInput > EnInput ) {
    10.             StartNum = EnInput;
    11.             EndNum = StInput;
    12.         }
    13.         else {
    14.             StartNum = StInput;
    15.             EndNum = EnInput;
    16.         }
    17.         MaxCycle = 0;
    18.         for( i = StartNum; i <= EndNum; i++ ) {
    19.             RetValue = Calculation( i );
    20.             if( RetValue > MaxCycle )
    21.                 MaxCycle = RetValue;
    22.         }
    23.         printf( "%d %d %d\n", StInput, EnInput, MaxCycle );
    24.     }
    25.     return 0;
    26. }
    27. int Calculation( int number )
    28. {
    29.     int CycleLength;
    30.     for( CycleLength = 1; number != 1; CycleLength++ ) {
    31.         if( number == 1 )
    32.             break;
    33.         if( number % 2 == 1 ) {
    34.             number = number * 3 + 1;
    35.         }
    36.         else {
    37.             number = number >> 1;
    38.         }
    39.     }
    40.     return CycleLength;
    41. }


크리에이티브 커먼즈 라이센스
Creative Commons License

"Algorithm / Acm" 분류의 다른 글

LCD Display (3)2008/02/17
Maximum Sum (0)2008/02/17
The Skyline Problem (2)2008/02/17
Ecological Bin Packing (0)2008/02/17
The Blocks Problem (0)2008/02/17
2008/02/17 08:30 2008/02/17 08:30
TAG , , ,

트랙백 주소 :: http://www.filewiki.net/tc/trackback/3

댓글을 달아 주세요