zoukankan      html  css  js  c++  java
  • 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()
    	商: " + arr[0] + "
    	余数: " + arr[1]);
            
        }
    }
  • 相关阅读:
    openJudge计算概论-谁考了第k名
    OpenJudge计算概论-求平均年龄
    OpenJudge计算概论-能被3,5,7整除的数
    OpenJudge计算概论-计算书费
    OpenJudge计算概论-计算三角形面积【海伦公式】
    OpenWrt 中安装配置Transmission
    OpenWrt中wifidog的配置及各节点页面参数
    Linux中后台执行任务
    通过ionice和nice降低shell脚本运行的优先级
    OpenWrt中对USB文件系统的操作, 以及读写性能测试
  • 原文地址:https://www.cnblogs.com/slothrbk/p/7251903.html
Copyright © 2011-2022 走看看