大数 The Chosen One

题目链接:https://nanti.jisuanke.com/t/A1535

题意:给你n(2=<n<=1e50)个人,将他们排成一列,每次剔除掉奇数位的,求留在最后的那个人序号为多少

分析:这个题根据样例可以很明显看出答案就是最接近n的2的幂次,写这个题是为了熟悉大数模板的使用

很简单的题当时死活出不了结果,这次记得注意,a.multiply(b)并不会改变a的值,而只是返回两者相乘结果

import java.io.*;
import java.util.*;
import java.math.*;

public class Main {
    public static void main(String args[]) {
        Scanner cin = new Scanner(System.in);
        int T=cin.nextInt();
        for(int i=0;i<T;i++) {
            BigInteger n,a=new BigInteger("1");
            BigInteger two=new BigInteger("2");
            n = cin.nextBigInteger();
            BigInteger c=a.multiply(two);
            while(c.compareTo(n) < 0) {
                c=a.multiply(two);
                a=c;
            }
            if(c.compareTo(n) == 0) {
                System.out.println(c);
            }
            else System.out.println(c.divide(two));
        }
    }
}

 

posted @ 2019-08-15 19:15  清酒令  阅读(146)  评论(0编辑  收藏  举报