蓝桥杯第七届真题 :冰雹数

点击查看:蓝桥杯历年真题 题解目录

冰雹数

蓝桥杯第七届真题 :冰雹数_第1张图片

思路分析:
1. 这道题很简单
3. 系统的评测数据最大为 999999 也是秒出结果
4. 为防止数据溢出,采用BigInteger
package java_2016_C;
import java.math.BigInteger;
import java.util.Scanner;
public class Main008_冰雹数 {
   public static void main(String[] args) {
      Scanner in = new Scanner(System.in);
      int n = in.nextInt();
      BigInteger zero = BigInteger.ZERO;
      BigInteger one = BigInteger.ONE;
      BigInteger two = BigInteger.valueOf(2);
      BigInteger three = BigInteger.valueOf(3);
      BigInteger b = BigInteger.valueOf(n);
      BigInteger max = BigInteger.valueOf(n);
      if(n==1) {
         System.out.println(4);
         return;
      }
      
      while((b.compareTo(one))!=0) {
         if((b.mod(two)).equals(zero)) {
            if((b.compareTo(max))==1) max=b;
            b=b.divide(two);
         }else {
            b=b.multiply(three)add(one);
            if(b.compareTo(max)==1) max=b;
         }
      }
      System.out.println(max);
   }
}

你可能感兴趣的:(蓝桥杯历届真题)