andy6804tw
7/15/2016 - 11:05 AM

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=36 困難度 ★ 這題是利用題目給的演算法算出兩個數字之間最大的數

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=36 困難度 ★ 這題是利用題目給的演算法算出兩個數字之間最大的數

import java.util.*;
 
public class Main {
 
 public static void main(String[] args) {
  Scanner scn = new Scanner(System.in);
  while (scn.hasNext()) {
   int a = scn.nextInt(), b = scn.nextInt(), max = 1, temp = 0;
   System.out.printf("%d %d", a, b);
   if (a > b) {
    temp = a;
    a = b;
    b = temp;
   }
   for (int i = b; i >= a; i--) {
    int num = i, j = 1;
    while (num != 1) {
     if (num % 2 == 0)
      num /= 2;
     else
      num = num * 3 + 1;
     j++;
 
    }
    if (max < j)
     max = j;
   }
   System.out.printf(" %d\n", max);
  }
 
 }
 /* 
    題目:Q100: The 3n + 1 problem
    作者:1010
    時間:西元 2016 年 5 月 */
}