poj2305 JAVA大数运算

来源:互联网 发布:排序二叉树的建立c语言 编辑:程序博客网 时间:2024/05/17 04:00
描述:给一个数b,两b进制大数p,m,求p mod m b进制结果。
String st = Integer.toString(num, base); // 把num当做10进制的数转成base进制的st(base <= 35).
int num = Integer.parseInt(st, base); // 把st当做base进制,转成10进制的int(parseInt有两个参数,第一个为要转的字符串,第二个为说明是什么进制).  
BigInter m = new BigInteger(st, base); // st是字符串,base是st的进制.
如果要将一个大数以2进制形式读入 可以使用cin.nextBigInteger(2);
当然也可以使用其他进制方式读入;
如果要将一个大数转换成其他进制形式的字符串 使用cin.toString(2);//将它转换成2进制表示的字符串

Code:
import java.math.*;
import java.util.Scanner;
public class Main
{
public static void main(String args[])
{
Scanner cin=new Scanner(System.in);
int b,i,j;
BigInteger p,m;
while(cin.hasNext())
{
b=cin.nextInt();
if(b==0)
break;
p=cin.nextBigInteger(b);
m=cin.nextBigInteger(b);
System.out.println(p.mod(m).toString(b));
}
}
}