Wednesday, July 2, 2014

UVa - 100 - The 3n + 1 problem


  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. using namespace std;
  5. int main(){
  6.     int n, m, i, j, temp, x, y;
  7.     int maximum;
  8.     while(scanf("%d %d"&x, &y) == 2){
  9.             cout << x << " " << y;
  10.             m = max(x, y);
  11.             n = min(x, y);
  12.         maximum = 0;
  13.     for(= n; i <= m; i++){
  14.             int count = 1;
  15.             j = i;
  16.             while(> 1){
  17.             if(% 2 == 0)
  18.                 j = j / 2;
  19.             else
  20.                 j = (3 * j + 1);
  21.             count++;
  22.         }
  23.         if(count >= maximum)
  24.             maximum = count;
  25.     }
  26.     cout << " " << maximum << endl;
  27.     }
  28.     return 0;
  29. }

No comments:

Post a Comment