1. 程式人生 > >java 大數的學習

java 大數的學習

vid 字符串 [] imp gen system.in 進制 light brush

import java.math.*;
import java.util.*;

public class study {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner cin = new Scanner(System.in);
		BigInteger a, b;
		
		while(cin.hasNext()) { //循環讀入,到文件尾
			a = cin.nextBigInteger();
			b = cin.nextBigInteger();
			System.out.println(a.add(b)); // 加
			System.out.println(a.subtract(b)); //減
			System.out.println(a.multiply(b)); // 乘
			System.out.println(a.divide(b)); // 除
			System.out.println(a.remainder(b)); //取模
			
			// 大整數比較
			if (a.compareTo(b) == 0) System.out.println("a == b");
			else if (a.compareTo(b) > 0) System.out.println("a > b");
			else if (a.compareTo(b) < 0) System.out.println("a < b");
			
			//大整數絕對值
			System.out.println(a.abs()); //大整數a的絕對值
			
			//大整數的冪
			int p = 10;
			System.out.println(a.pow(p)); //大整數a的p次冪
			
			//返回大整數10進制表示的字符串
			System.out.println(a.toString());
			
			//返回大整數k進制的字符串表示
			int k = 8;
			System.out.print(a.toString(k));
		}
	}

}

java 大數的學習