java大数基本用法

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 

import java.math.BigInteger;
import java.util.Scanner;

public class Main {
    
    public static void main(String[] args) {
        BigInteger sum = new BigInteger("1");
        Scanner input   = new Scanner(System.in);
        while(true)
        {
            BigInteger num = input.nextBigInteger();
            if(num .equals(BigInteger.ZERO ) ) break;
            else sum = sum.multiply(num);
        }
        int cnt = 0;
        System.out.println(sum);
        while(true )
        {
            BigInteger[] bis = sum.divideAndRemainder(BigInteger.TEN);
            if(bis[1].equals(BigInteger.ZERO))
            {
                cnt++;
                sum = bis[0];
            }
            else break;
            
            
        }
        System.out.println(cnt);
            
    

    }
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值