fork(1) 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(2, 0); cache[1] = 1;
  15.  
  16. if(n >= cache.size()) cache.resize(n+1, 0);
  17. if(cache[n] == 0) cache[n] = getLength(getNextTerm(n))+1;
  18. return cache[n];
  19. }
  20.  
  21. int main()
  22. {
  23. int i, j; while(cin >> i >> j) {
  24. int currMax = 0;
  25. for(int n = min(i, j); n <= max(i, j); n++)
  26. currMax = max(currMax, getLength(n));
  27. cout << i << ' ' << j << ' ' << currMax << '\n';
  28. }
  29. return 0;
  30. }
Runtime error #stdin #stdout 0s 4236KB
stdin
1 10
100 200
201 210
900 1000
stdout
1 10 20
100 200 94
201 210 89