題意:輸入一個進位b,在輸入兩個基於b進位的大整數 x,y ,求x%y的b進位結果。
http://162.105.81.212/JudgeOnline/problem?id=2305
函數:
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進位表示的字串
import java.math.*; import java.util.*;import java.math.BigInteger;public class Main{ public static void main (String[] args)throws Exception { Scanner in=new Scanner(System.in); int b; BigInteger x,y; while(in.hasNextInt()) { b=in.nextInt(); if(b==0) break; x=in.nextBigInteger(b); y=in.nextBigInteger(b); x=x.mod(y); System.out.println(x.toString(b)); } } }