ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2021-05-07 11:01:45  阅读:188  来源: 互联网

标签:aa represent BigInteger -- Regional Base62 base now


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'));
        }
    }
}

标签:aa,represent,BigInteger,--,Regional,Base62,base,now
来源: https://www.cnblogs.com/lipoicyclic/p/14738221.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有