DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

ACM Q100: The 3n + 1 Problem

01.19.2007
| 4934 views |
  • submit to reddit
        // description of your code here

// insert code here..
#include <stdio.h>

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;
}

    
    Tags:
  • C