浅谈java中bigInteger用法

1.赋值:

BigInteger a=new BigInteger("1");

BigInteger b=BigInteger.valueOf(1);

2.运算:

① add(); 大整数相加 
BigInteger a=new BigInteger(“23”); 
BigInteger b=new BigInteger(“34”); 
a. add(b);

②subtract(); 相减 
③multiply(); 相乘 
④divide(); 相除取整 
⑤remainder(); 取余 
⑥pow(); a.pow(b)=a^b 
⑦gcd(); 最大公约数 
⑧abs(); 绝对值 
⑨negate(); 取反数 
⑩mod(); a.mod(b)=a%b=a.remainder(b); 

3.BigInteger构造函数: 
一般用到以下两种: 
BigInteger(String val); 
将指定字符串转换为十进制表示形式; 
BigInteger(String val,int radix); 
将指定基数的 BigInteger 的字符串表示形式转换为 BigInteger 
4.基本常量: 
A=BigInteger.ONE 1 
B=BigInteger.TEN 10 
C=BigInteger.ZERO 0 

5.n.compareTo(BigInteger.ZERO)==0  //相当于n==0

6.if(a[i].compareTo(n)>=0 &&a[i].compareTo(m)<=0)   // a[i]>=n && a[i]<=m 

 1 import java.math.BigInteger;
 2 import java.util.Scanner;
 3 
 4 public class Main {
 5     
 6     public static void main(String[] args) {
 7         BigInteger sum = new BigInteger("1");
 8         Scanner input   = new Scanner(System.in);
 9         while(true)
10         {
11             BigInteger num = input.nextBigInteger();
12             if(num .equals(BigInteger.ZERO ) ) break;
13             else sum = sum.multiply(num);
14         }
15         int cnt = 0;
16         System.out.println(sum);
17         while(true )
18         {
19             BigInteger[] bis = sum.divideAndRemainder(BigInteger.TEN);
20             if(bis[1].equals(BigInteger.ZERO))
21             {
22                 cnt++;
23                 sum = bis[0];
24             }
25             else break;
26             
27             
28         }
29         System.out.println(cnt);
30             
31     
32 
33     }
34 }
原文地址:https://www.cnblogs.com/unknownname/p/8823887.html