poj 2305 java b進位取餘

來源:互聯網
上載者:User

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 }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.