BigInteger與String之間的轉換與2-36位進制轉換

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
import java.util.LinkedHashSet;
import java.util.Iterator;
import java.io.*;
import java.applet.*;
import  java.util.Collections.*;
public class Main {
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        int b;
        b = sc.nextInt();
        String t1=sc.next(),t2=sc.next();
        //從原進制轉換爲10進制
        String s1 = new BigInteger(t1,b).toString(10);
        String s2 = new BigInteger(t2,b).toString(10);
        //兩個十進制的大數相加
        BigInteger x,y;
        x = new BigInteger(s1);
        y = new BigInteger(s2);
        x = x.add(y);
        //將相加結果轉換爲原進制
        String ans=new BigInteger(x.toString(),10).toString(b);
        System.out.println(ans.toUpperCase());
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章