AcWing
  • 首页
  • 活动
  • 题库
  • 竞赛
  • 应用
  • 更多
    • 题解
    • 分享
    • 商店
    • 问答
    • 吐槽
  • App
  • 登录/注册

AcWing 3395. 10进制 VS 2进制 0.04 AC币

作者: 作者的头像   LaLa_JUROU ,  2023-05-23 12:12:57 ,  所有人可见 ,  阅读 10


0


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

public class Main{
    static Scanner in = new Scanner(System.in);

    static String[] dp = new String[10001];
    static BigInteger A,B,C = new BigInteger("2");
    public static void main(String[] args) {
        A = new BigInteger(in.nextLine());
        B = new BigInteger("0");
        char[] binary = toBinary(A).toCharArray();
        for(int i = 0; i < binary.length; i ++) {
             BigInteger result = new BigInteger(binary[i] + "");
             result = result.multiply(new BigInteger(BinaryNumber(i + 1)));
             B = B.add(result);
        }
        System.out.println(B.toString());
    }
    static String BinaryNumber(int i) {//dp优化
        if(i == 1) {
            dp[i] = "1";
            return "1";
        } else {
            dp[i] = new BigInteger(dp[i - 1]).multiply(new BigInteger("2")).toString();
            return dp[i];
        }   
    }
    static String toBinary(BigInteger A) {
        StringBuilder binary = new StringBuilder();
        while(A.toString().length() > 1 || !A.toString().equals("0")) {
            binary.append(A.divideAndRemainder(C)[1]);
            A = A.divide(C);
        }
        return binary.reverse().toString();//反转加转换为字符串
    }
}

0 评论

你确定删除吗?
1024
x

© 2018-2023 AcWing 版权所有  |  京ICP备17053197号-1
用户协议  |  隐私政策  |  常见问题  |  联系我们
AcWing
请输入登录信息
更多登录方式: 微信图标 qq图标 qq图标
请输入绑定的邮箱地址
请输入注册信息