• 2019 ICPC Asia Yinchuan Regional I. Base62(高精度/BigInteger)


    As we already know, base64 is a common binary-to-text encoding scheme. Here we define a special series of positional systems that represent numbers using a base (a.k.a. radix) of 2 to 62. The symbols '0' -- '9' represent zero to nine, and 'A' -- 'Z' represent ten to thirty-five, and 'a' -- 'z' represent thirty-six to sixty-one. Now you need to convert some integer z in base x into base y.

    Input

    The input contains three integers x, y (2≤x,y≤62) and z(0≤z<x120), where the integer z is given in base x.

    Output

    Output the integer zz in base yy.

    样例输入复制

    16 2 FB
    

    样例输出复制

    11111011
    

    简单的进制转换题目,不过需要用高精。C++的高精模版有可能会爆,所以可以用Java的大整数类来写(真香

    注意判一下输入的z是0的情况。

    import java.math.BigInteger;
    import java.util.ArrayList;
    import java.util.Scanner;
    public class Main {
        public static int get(char c) {
            if(c <= '9') return (c - '0');
            else if(c <= 'Z') return 10 + (c - 'A');
            else return 36 + (c - 'a');
        }
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            BigInteger base1, base2, a;
            String s;
            base1 = sc.nextBigInteger();
            base2 = sc.nextBigInteger();
            s = sc.next();
            a = BigInteger.ZERO;
            for(int i = 0; i < s.length(); i++) {
                char now = s.charAt(i);
                a = a.multiply(base1);
                String xx = String.valueOf(get(now));
                BigInteger x = new BigInteger(xx);
                a = a.add(x);
            }
            if(a == BigInteger.ZERO) {
                System.out.println("0");
                return;
            }
            ArrayList<BigInteger> arr = new ArrayList<>();
            while(a != BigInteger.ZERO) {
                BigInteger aa = a;
                aa = aa.divide(base2);
                aa = aa.multiply(base2);
                BigInteger now = a.subtract(aa);
                arr.add(now);
                a = a.divide(base2);
            }
    
            for(int i = arr.size() - 1; i >= 0; i--) {
                BigInteger tmp = arr.get(i);
                int now = tmp.intValue();
                if(now <= 9) System.out.print(now);
                else if(now <= 35) System.out.print((char)(now - 10 + 'A'));
                else System.out.print((char)(now - 36 + 'a'));
            }
        }
    }
    
    
  • 相关阅读:
    查看CentOS版本方法
    Android中string.xml文件中设置部分字体颜色大小
    linux重启oracle 各种方法
    Duilib初级控件扩展一例: 具有鼠标滚动消息的OptionUI
    VirtualBox 安装XP虚拟机需要注意的问题
    如何让VS2012编写的程序在XP下运行
    VC 系统托盘编程,含有气泡提示
    获取文件或是文件夹的大小和占用空间
    VC++实现获取文件占用空间大小的两种方法(非文件大小)
    VC++ 获取文件属性创建时间、修改时间和访问时间
  • 原文地址:https://www.cnblogs.com/lipoicyclic/p/14738221.html
Copyright © 2020-2023  润新知