2019ICPC 银川区域赛 Base62(进制转换 java)

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 22 to 6262. The symbols ‘00’ – ‘99’ 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 zz in base xx into base yy.

Input
The input contains three integers x, y~(2 \le x, y \le 62)x,y (2≤x,y≤62) and z~(0 \le z < x^{120})z (0≤z<x
120
), where the integer zz is given in base xx.

Output
Output the integer zz in base yy.

样例输入复制
16 2 FB
样例输出复制
11111011

题意: 进制转换,最大62进制

思路:
java练习题,现场赛的时候用C++写这题差点打了铁┭┮﹏┭┮。

package tdm;

import java.math.BigInteger;
import java.util.*;
public class Main{
    public static int get(char c) {
        if(c >= '0' && c <= '9')return (c - '0');
        if(c >= 'A' && c <= 'Z')return (c - 'A') + 10;
        if(c >= 'a' && c <= 'z')return (c - 'a') + 36;
        return -1;
    }

    public static char trans(int c) {
        if(c >= 0 && c <= 9)   return (char)(c + '0');
        if(c >= 10 && c <= 35) return (char)(c - 10 + 'A');
        if(c >= 36 && c <= 61) return (char)(c - 36 + 'a');
        return 'F';
    }

    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        int a,b; a = cin.nextInt(); b = cin.nextInt();
        char []res = new char[1000];
        String s; s = cin.next();
        BigInteger ans = BigInteger.ZERO;
        BigInteger Zero = BigInteger.ZERO;
        BigInteger remain = BigInteger.ZERO;
        for(int i = 0;i < s.length();i++) {
            ans = ans.multiply(BigInteger.valueOf(a));
            char c = s.charAt(i);
            ans = ans.add(BigInteger.valueOf(get(c)));
        }

        if(ans == Zero) {
            System.out.print(0);
            return;
        }
        int cnt = 0;
        while(ans != Zero) {
            remain = ans.mod(BigInteger.valueOf(b));
            ans = ans.divide(BigInteger.valueOf(b));
            res[++cnt] = trans(remain.intValue());
        }

        for(int i = cnt;i >= 1;i--) {
            System.out.print(res[i]);
        }
    }
}
发布了756 篇原创文章 · 获赞 27 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/tomjobs/article/details/104646813
今日推荐