fork download
  1. #include <stdio.h>
  2.  
  3. int main(void) {
  4. int t;
  5. scanf("%d", &t);
  6. for (int i = 0; i < t; i++) {
  7. int x, y;
  8. scanf("%d%d", &x, &y);
  9. int first = x, second = x;
  10. while (x != 1 && x < y) {
  11. if (x % 2 == 0)
  12. x /= 2;
  13. else if (x % 2 == 1)
  14. x = 3 * x + 1;
  15. if (x > first) {
  16. second = first;
  17. first = x;
  18. } else if ((x < first && x > second) || (x < first && first == second))
  19. second = x;
  20. //printf("x = %d, first = %d, second = %d\n", x, first, second);
  21. }
  22. //printf("\n");
  23. printf("%d\n", second);
  24. }
  25. return 0;
  26. }
  27.  
Success #stdin #stdout 0s 5296KB
stdin
2
12 20
19 70
stdout
12
58