现在的位置: 首页 > 综合 > 正文

poj 2305 java b进制取余

2013年02月28日 ⁄ 综合 ⁄ 共 802字 ⁄ 字号 评论关闭

http://hi.baidu.com/acmdearway/blog/item/e08db230563e3d13ebc4afa2.html

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的进制.

//Added by abilitytao
1.如果要将一个大数以2进制形式读入 可以使用cin.nextBigInteger(2); 
当然也可以使用其他进制方式读入;
2.如果要将一个大数转换成其他进制形式的字符串 使用cin.toString(2);//将它转换成2进制表示的字符串

View Code

 1 import java.math.*;
2 import java.io.*;
3 import java.util.*;
4 public class Main{
5 public static void main(String args[])
6 {
7 Scanner cin=new Scanner(System.in);
8 BigInteger p,m,ans;
9 String str;
10 int b;
11 while(cin.hasNext())
12 {
13 b=cin.nextInt();
14 if(b==0) break;
15 p=cin.nextBigInteger(b);
16 m=cin.nextBigInteger(b);
17 ans=p.mod(m);
18 str=ans.toString(b);
19 System.out.println(str);
20 }
21 }
22 }

抱歉!评论已关闭.