java大数常用的方法

//创建大数对象:
BigInteger a=new BigInteger("123");
BigInteger a=BigInteger.valueOf();
//常用方法: multiply(); //大数* add();   //大数+ divide();   //大数/ subtract(); //大数- divideAndRemainder();//返回一个数组key=0存放商值,key=1存放余数 compareTo();   //比较,判断2.00与2.0相等 equals();   //比较,判断2.00与2.0不相等 gcd(); //最大公约数 max(); min(); pow(); mod(); //取余 toString(); ZERO; //常量0 valueOf(); toPlainString(): //返回不带指数字段的此 BigDecimal 的字符串表示形式 stripTrailingZeros() //去尾零

//字符串 substring()    //子串 str=str.substring(int beginIndex);//截取掉str从首字母起长度为beginIndex的字符串,将剩余字符串赋值给str; str=str.substring(int beginIndex,int endIndex);//截取str中从beginIndex开始至endIndex结束时的字符串,并将其赋值给str;

 示例:

//样例
import java.util.Scanner;
import java.math.*;

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner cin = new Scanner(System.in);
        /*
         * 输入
         * */
//        BigInteger a = cin.nextBigInteger();//明确输入类型,大整数类型输入小数抛出异常InputMismatchException
//        BigDecimal b = cin.nextBigDecimal();//浮点数类
        
        /*
         * 初始化
         * */
//        BigInteger c= BigInteger.valueOf(0);//初始化为0
//        c = BigInteger.ZERO;//初始化为0
//        BigInteger a = new BigInteger("123");
        
        /*
         * 字符串初始化
         * */
//        String str = cin.nextLine();//或者str = cin.next();
//        a = new BigInteger(str);
        
        /*字符串常用方法*/
        BigInteger num1 = new BigInteger("99999999999999999999999999999");
        BigInteger num2 = new BigInteger("11111111111111111111111111111");
        
        //+
        System.out.println("+: " + num1.add(num2));
        //-
        System.out.println("-: " + num1.subtract(num2));
        //
        System.out.println("*: " + num1.divide(num2));
        //*
        System.out.println("/: " + num1.multiply(num2));
        //mod()取余数
        System.out.println("mod(): " + num1.add(BigInteger.valueOf(31)).mod(num2));
        //divideAndRemainder()返回一个数组key=0存放商值,key=1存放余数
        BigInteger arr[] = num1.divideAndRemainder(num2);
        System.out.println("divideAndRemainder()\n\t商: " + arr[0] + "\n\t余数: " + arr[1]);
        
    }
}

 

posted @ 2017-02-11 12:13  朤尧  阅读(496)  评论(0编辑  收藏  举报