I.java 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package contest1400;
  2. import java.io.PrintWriter;
  3. import java.util.Comparator;
  4. import java.util.Scanner;
  5. public class I {
  6. static Scanner cin = new Scanner(System.in);
  7. static PrintWriter cout = new PrintWriter(System.out);
  8. public static void main(String[] args) {
  9. while (cin.hasNext()) {
  10. int from = cin.nextInt();
  11. int to = cin.nextInt();
  12. int tot = foo(from, to);
  13. cout.println(String.format("%d %d %d", from, to, tot));
  14. }
  15. cout.close();
  16. cin.close();
  17. }
  18. private static int foo(int from, int to) {
  19. if (from > to) {
  20. int temp = from;
  21. from = to;
  22. to = temp;
  23. }
  24. int max = 0;
  25. for (int n = from; n <= to; ++n) {
  26. int cur = f(n);
  27. if (cur > max) max = cur;
  28. }
  29. return max;
  30. }
  31. private static int f(int n) {
  32. int step = 1;
  33. while (n > 1) {
  34. if (n % 2 == 0)
  35. n /= 2;
  36. else n = 3 * n + 1;
  37. ++step;
  38. }
  39. return step;
  40. }
  41. }