UVA 100 Java

  1. import java.util.*;
  2.  
  3. public class main{
  4. public static void main(String[] args) {
  5. Scanner sc=new Scanner(System.in);
  6. while(sc.hasNextInt())
  7. {
  8. int num1 = sc.nextInt();
  9. int num2 = sc.nextInt();
  10. int max_n = Math.max(num1, num2);
  11. int min_n = Math.min(num1, num2);
  12. int max = 0;
  13. for(int i = min_n; i <= max_n; i++)
  14. {
  15. int n = i;
  16. int count = 1;
  17. while(n != 1)
  18. {
  19. count++;
  20. if(n%2 == 1)
  21. n=3*n+1;
  22. else
  23. n = n / 2;
  24. }
  25. if(count > max)
  26. max = count;
  27. }
  28. System.out.printf("%d %d %d\r\n", num1, num2, max);
  29. }
  30. }
  31. };
  32.  

留言

這個網誌中的熱門文章

C# 井字遊戲