ICPC Asia Nanning 2017 F. The Chosen One (高精度运算)

题目链接:The Chosen One

比赛链接:ICPC Asia Nanning 2017

题意

\(t\) 组样例,每组给出一个整数 \(n(2\le n\le 10^{50})\),求不大于 \(n\) 的最大的 \(2\) 的整数次幂。

题解

高精度运算

Java BigInteger 中的 bitLength() 方法可以直接计算某个大数二进制表示下的位数。

更多关于 Java BigInteger 的操作参见我的另一篇文章 大数运算之 Java BigInteger 的基本用法

import java.util.Scanner;
import java.math.BigInteger;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int t = in.nextInt();
        while (t-->0){
            BigInteger n = in.nextBigInteger();
            BigInteger ans = new BigInteger("2");
            System.out.println(ans = ans.pow(n.bitLength() - 1));
        }
    }
}
posted @ 2019-08-15 20:58  wuli涛涛  阅读(225)  评论(0编辑  收藏  举报