Base62(高精度 进制转换)

Base62

题目

在这里插入图片描述

题意

将一个 x 进制的 z 转换成 y 进制的数并输出。

AC代码
#include <cstdio>
#include <cstring>
#include <string>

using namespace std;

void output(int a) {
    if (a > 9) {
        output(a / 10);
    }
    putchar(a % 10 + '0');
}

const int maxn = 1000;
int t[maxn], A[maxn];
char str1[maxn], str2[maxn];
int x, y;

int getNum(char n) {
    int num;
    if (n >= 'a' && n <= 'z') {
        num = 36 + n - 'a';
    } else if (n >= 'A' && n <= 'Z') {
        num = 10 + n - 'A';
    } else {
        num = n - '0';
    }
    return num;
}

char getChar(int n) {
    char c;
    if (n >= 36 && n <= 61) {
        c = 'a' + n - 36;
    } else if (n >= 10 && n <= 35) {
        c = 'A' + n - 10;
    } else {
        c = '0' + n;
    }
    return c;
}

void solve() {
    int len = strlen(str1);
    for (int i = len; i >= 0; --i) {
        t[len - 1 - i] = getNum(str1[i]);
    }
    int k = 0;
    while (len) {
        for (int i = len; i >= 1; --i) {
            t[i - 1] += t[i] % y * x;
            t[i] /= y;
        }
        A[k++] = t[0] % y;
        t[0] /= y;
        while (len > 0 && !t[len - 1]) {
            len--;
        }
    }
    str2[k] = NULL;
    for (int i = 0; i < k; i++) {
        str2[k - 1 - i] = getChar(A[i]);
    }
}

int main() {
    scanf("%d%d%s", &x, &y, str1);
    solve();
    printf("%s\n", str2);
    return 0;
}
插入 36 位以内的高精度操作

将36位的数字s,转换为10位的数字

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

public class Main {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        String s = sc.next();
        System.out.println(new BigInteger(s, 36).toString(10));
        sc.close();
    }
}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值