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