Created
January 11, 2012 15:32
-
-
Save kuoe0/1595194 to your computer and use it in GitHub Desktop.
[UVa] 100 – The 3n+1 Problem - http://kuoe0.ch/18/uva-100-the-3n-plus-1-problem/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <cstdio> | |
#include <cstdlib> | |
using namespace std; | |
long long int MAX; | |
long long int count( int x ) { | |
if ( x == 1 ) | |
return 0; | |
return count( x == 1 ? 3 * x + 1 : ( x >> 1 ) ) + 1; | |
} | |
int main() { | |
int a, b; | |
while ( ~scanf( "%d %d", &a, &b ) ) { | |
printf( "%d %d", a, b ); | |
if ( a > b ) | |
swap( a, b ); | |
MAX = 0; | |
for ( int i = a; i < = b; ++i ) | |
MAX = max( MAX, count( i ) + 1 ); | |
printf( " %lldn", MAX ); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment