poj1220

本文介绍了一种使用Java实现的高精度进制转换方法。该方法可以将任意基数的数转换为另一种任意基数的数,并且能处理非常大的数字,确保了转换过程中的精确性。代码中使用了BigInteger类来处理大数运算,通过定义两个辅助函数进行数值和字符之间的转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

高精度进制转换,java做

ContractedBlock.gifExpandedBlockStart.gifView Code
import java.io.*;
import java.util.*;
import java.math.*;

public class Main {
static int cal(char ch)
{
if (ch >= 'A' && ch <= 'Z')
return (int)(ch - 'A' + 10);
if (ch >= 'a' && ch <= 'z')
return (int)(ch - 'a' + 36);
return (int)(ch - '0');
}
static char get(long l)
{
if (l <= 9)
return (char)(l + '0');
if (l <= 35)
return (char)(l - 10 + 'A');
return (char)(l - 36 + 'a');
}
static public void main(String[] args) throws FileNotFoundException
{
Scanner cin = new Scanner(new BufferedInputStream(System.in));
//Scanner cin = new Scanner(new FileInputStream("t.txt"));
int t = cin.nextInt();
while (t-- != 0)
{
int a = cin.nextInt();
int b = cin.nextInt();
String st = cin.next();
BigInteger ans = new BigInteger("0");
for (int i = 0; i < st.length(); i++)
ans = ans.multiply(BigInteger.valueOf(a)).add(BigInteger.valueOf(cal(st.charAt(i))));
//System.out.println(ans);
String st1 = new String("");
while (!ans.equals(BigInteger.valueOf(0)))
{
st1 = get(ans.mod(BigInteger.valueOf(b)).longValue()) + st1;
ans = ans.divide(BigInteger.valueOf(b));
}
System.out.println(a + " " + st);
if (st1.equals(""))
System.out.println(b + " 0");
else
System.out.println(b + " " + st1);
System.out.println();
}
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值