2016年7月3日 星期日

Q100: The 3n + 1 problem


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 月 */
}


困難度 
這題是利用題目給的演算法算出兩個數字之間最大的數

沒有留言:

張貼留言