大整数演算(java BigIntegerクラス)
1424 ワード
JAva大整数クラスの演算には、加算、減算、乗算、除算、余剰数の求め、比較サイズ、高精度べき乗、max()関数、min()関数、abs()関数、gcd()関数などが含まれることが多い.このような問題が発生した場合、CまたはC++で解決するのは面倒ですが(できるだけ把握しなければなりません)、javaは非常に便利なBigIntegerクラス(大整数クラス)を提供してくれます.
import java.util.Scanner;
import java.math.*;
public class test1 {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
BigInteger a,b,c;
while(cin.hasNext()) {
a = cin.nextBigInteger();
b = cin.nextBigInteger();
c = cin.nextBigInteger();
System.out.println(a.add(b)); //add()
System.out.println(a.subtract(b)); //substract()
System.out.println(a.multiply(b)); //multiply()
System.out.println(a.divide(b)); //divide()
System.out.println(a.mod(b)); // mod()
System.out.println(a.gcd(b)); //gcd() a b
System.out.println(c.abs()); //
System.out.println(a.max(b)); //max() a、b
System.out.println(a.min(b)); //min() a、b
System.out.println(a.equals(b)); //a、b , true, false
}
cin.close();
}
}
多次元配列の初期化:BigInteger f[] = new BigInteger[110];
BigInteger c[][] = new BigInteger[110][110];
for ( int i = 0 ; i <= 100 ; i++ )
for ( int j = 0 ; j <= 100 ; j++ ) c[i][j] = BigInteger.valueOf(0); //
大整数類の問題は基本的にこれらに出会って、javaのBigInteger類を掌握して、このような問題は基本的にすべて水が流れて、C/C++の実現過程はここで先にコードしません.