ACM Q100: The 3n + 1 Problem
Join the DZone community and get the full member experience.
Join For Free// description of your code here
// insert code here..
#include
void cal(int m , int n ){
int k ;
int num , times=1 ;
int MAX = 0 ;
for( k = m ; k <= n ; k++ ){
num = k ;
while( num != 1){
if( num % 2 == 0 ) {
num /= 2;
}
else {
num = 3*num+1 ;
}
times ++ ;
}
if(times > MAX){
MAX = times ;
}
times = 1 ;
}
printf("%d\n", MAX ) ;
}
int main() {
int i , j ;
while((scanf( "%d%d" , &i , &j ))==2){
printf("%d %d ", i , j ) ;
if( i < j ) {
cal( i , j ) ;
}
else{
cal( j , i ) ;
}
}
return 0;
}
Opinions expressed by DZone contributors are their own.
Comments