fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. int getNextTerm(int n)
  7. {
  8. if(n%2 == 0) return n/2;
  9. else return 3*n+1;
  10. }
  11.  
  12. int getLength(int n)
  13. {
  14. static vector<int>cache(1000001, 0); cache[1] = 1;
  15.  
  16. if(n >= 1000000) return getLength(getNextTerm(n))+1;
  17.  
  18. if(cache[n] == 0) cache[n] = getLength(getNextTerm(n))+1;
  19. return cache[n];
  20. }
  21.  
  22. int main()
  23. {
  24. int i, j; while(cin >> i >> j) {
  25. int currMax = 0;
  26. for(int n = min(i, j); n <= max(i, j); n++)
  27. currMax = max(currMax, getLength(n));
  28. cout << i << ' ' << j << ' ' << currMax << '\n';
  29. }
  30. return 0;
  31. }
Success #stdin #stdout 0s 6768KB
stdin
1 10
100 200
201 210
900 1000
stdout
1 10 20
100 200 125
201 210 89
900 1000 174