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