7/11/2013

the 3n+1 Problem - 100 - UVA

  1. #include <iostream>
  2. #include<algorithm>
  3. using namespace std;
  4. int main ()
  5. {
  6.         int n , i , j;
  7.         while(cin>>i>>j)
  8.         {
  9.                 int tempi = i;
  10.                 int tempj = j;
  11.                 if(i>j)
  12.                         swap(i,j);
  13.                
  14.                 int maxCycle_Length = INT_MIN;
  15.                 int cycle_Length;
  16.                 while(i<=j)
  17.                 {
  18.                         n=i;
  19.                         cycle_Length = 1;
  20.                         while(n!=1){
  21.                                 if(n%2!=0)
  22.                                         n=(3*n)+1;
  23.                                 else
  24.                                         n = n/2;
  25.                                 cycle_Length++;
  26.                         }
  27.                         if(cycle_Length > maxCycle_Length)
  28.                                 maxCycle_Length = cycle_Length;
  29.                         i++;
  30.                 }
  31.                 cout<<tempi<<" "<<tempj<<" "<<maxCycle_Length<<endl;
  32.         }
  33.        
  34.         return 0;
  35. }


    You can find more of my solutions on
     Here

No comments:

Post a Comment