zoukankan      html  css  js  c++  java
  • Java大数BigInteger BigDecimal

    基本函数:(参考here

    import java.math.BigInteger;  
    import java.util.*;  
      
    public class Main  
    {  
        public static void main(String args[])  
        {  
            Scanner cin = new Scanner(System.in);
            
            //读到EOF
            while(cin.hasNext()){}
            //读入BigInteger
            BigInteger a = cin.nextBigInteger();
            
            //构造方法
            //将十进制字符串转化为BigInteger
            //public BigInteger(String val)
            BigInteger b = new BigInteger("3");
            //byte范围-128到+127 8为2进制数 c为767(1011111111)
            //public BigInteger(byte[] val)
            byte[] bt = new byte[]{2,-1};
            BigInteger c = new BigInteger(bt);
            //将radix进制的字符串转化为BigInteger
            //public BigInteger(String val, int radix)
            BigInteger d = new BigInteger("3", 2);
            //随机生成 0到2的numBits次方 -1的随机数
            //public BigInteger(int numBits, Random rnd)
            BigInteger e = new BigInteger(10, new Random());
            //signum为符号位 1为正 0为0 -1为负
            //public BigInteger(int signum, byte[] magnitude)
            BigInteger f = new BigInteger(-1, bt);
            //随机生成一个 长度为bitLength的 可能性大于(1-1/(2的certainty次方))是素数 的数
            //public BigInteger(int bitLength, int certainty, Random rnd)
            BigInteger g = new BigInteger(10, 1, new Random());
            
            //常量
            //0
            //public static final BigInteger ZERO
            a = BigInteger.ZERO;
            //1
            //public static final BigInteger ONE
            a = BigInteger.ONE;
            //10
            //public static final BigInteger TEN
            a = BigInteger.TEN;
            
            //静态方法
            //随机生成一个 长度为bitLength的可能是素数的数
            //public static BigInteger probablePrime(int bitLength, Random rnd)
            BigInteger.probablePrime(10, new Random());
            //值等于val的值
            //public static BigInteger valueOf(long val)
            BigInteger.valueOf(10);
            
            //加法a+b
            //public BigInteger add(BigInteger val)
            a.add(b);
            //减法a-b
            //public BigInteger subtract(BigInteger val)
            a.subtract(b);
            //乘法a*b
            //public BigInteger subtract(BigInteger val)
            a.multiply(b);
            //除法a/b
            //public BigInteger divide(BigInteger val)
            a.divide(b);
            //取模a%b b需大于0 5mod3=2 -5mod3=1
            //public BigInteger mod(BigInteger m)
            a.mod(b);
            //求余 5rem3=2 -5rem3=-2 5rem-3=2 -5rem-3=-2
            //public BigInteger remainder(BigInteger val)
            a.remainder(b);
            //[0]为a/b [1]为a%b
            //public BigInteger[] divideAndRemainder(BigInteger val)
            a.divideAndRemainder(b);
            
            //a==b?
            //public boolean equals(Object x)
            a.equals(b);
            //a的正负 正为1 0为0 负为-1
            //public int signum()
            a.signum();
            //绝对值|a|
            //public BigInteger abs()
            a.abs();
            //比较a>b返回1 a==b返回0 a<b返回-1
            //public BigInteger andNot(BigInteger val)
            a.compareTo(b);
            //相反数-a
            //public BigInteger negate()
            a.negate();
            //max(a,b)
            //public BigInteger max(BigInteger val)
            a.max(b);
            //min(a,b)
            //public BigInteger min(BigInteger val)
            a.min(b);
            //次方a的3次方
            //public BigInteger pow(int exponent)
            a.pow(3);
            //a的-1次方 %b
            //public BigInteger modInverse(BigInteger m)
            a.modInverse(b);
            //a的b次方 %c
            //public BigInteger modPow(BigInteger exponent,BigInteger m)
            a.modPow(b, c);
            
            //~a
            //public BigInteger not()
            a.not();
            //a^b
            //public BigInteger xor(BigInteger val)
            a.xor(b);
            //a|b
            //public BigInteger or(BigInteger val)
            a.or(b);
            //a&b
            //public BigInteger divide(BigInteger val)
            a.and(b);
            //a左移n位 (a << n)
            //public BigInteger shiftLeft(int n)
            a.shiftLeft(10);
            //a右移n位 (a >> n)
            //public BigInteger shiftRight(int n)
            a.shiftRight(10);
            //a&(~b)
            //public BigInteger andNot(BigInteger val)
            a.andNot(b);
            //二进制形式中把第n位二进制设为0 (a & ~(1<<n))
            //public BigInteger clearBit(int n)
            a.clearBit(10);
            //二进制形式中把第n位二进制设为1 (a | (1<<n))
            //public BigInteger setBit(int n)
            a.setBit(10);
            //二进制形式中第n位二进制是否为1 (a & (1<<n)) != 0)
            //public boolean testBit(int n)
            a.testBit(10);
            //二进制形式中把第n位二进制翻转 (a ^ (1<<n))
            //public BigInteger flipBit(int n)
            a.flipBit(10);
            //二进制形式中最低位1后面0的个数 (a == 0? -1 : log2(a & -a))
            //public int getLowestSetBit()
            a.getLowestSetBit();
            //二进制形式中与符号不同的位的数量 7为3 -7为2
            //public int bitCount()
            a.bitCount();
            //二进制形式中不包括符号位的长度
            //public int bitLength()
            a.bitLength();
            
            //a和b的最大公约数
            //public BigInteger gcd(BigInteger val)
            a.gcd(b);
            //a可能为素数返回true a一定为合数返回false 素数可能性大于(1-1/(2的certainty次方))
            //public boolean isProbablePrime(int certainty)
            a.isProbablePrime(10);
            //大于a的可能为素数的第一个整数。
            //public BigInteger nextProbablePrime()
            a.nextProbablePrime();
            //a的哈希码
            //public int hashCode()
            a.hashCode();
            
            //a的二进制补码形式
            //public byte[] toByteArray()
            a.toByteArray();
            //a的十进制字符串形式
            //public String toString()
            a.toString();
            //a的radix进制字符串形式
            //public String toString(int radix)
            a.toString(2);
            //将a转换为int
            //public int intValue()
            a.intValue();
            //将a转换为long
            //public long longValue()
            a.longValue();
            //将a转换为float
            //public float floatValue()
            a.floatValue();
            //将a转换为double
            //public double doubleValue()
            a.doubleValue();
            
            //JAVA 1.8
            a.byteValueExact();
            a.intValueExact();
            a.longValueExact();
            a.shortValueExact();
            
            //从类 java.lang.Number 继承的方法
            //将a转换为short
            //public short shortValue()
            a.shortValue();
            //将a转换为byte
            //public byte byteValue()
            a.byteValue();
            
            //从类 java.lang.Object 继承的方法
            //public final Class<?> getClass()
            a.getClass();
            //public final void notify()
            a.notify();
            //public final void notifyAll()
            a.notifyAll();
            try {
                //public final void wait() throws InterruptedException
                a.wait();
                //public final void wait(long timeout) throws InterruptedException
                a.wait(10);
                //public final void wait(long timeout, int nanos) throws InterruptedException
                a.wait(10, 10);
            } catch (Exception exception) {
            }
    
        }  
    }  

    方法摘要

    BigInteger

    abs()返回其值是此BigInteger的绝对值的BigInteger。

    BigInteger

    add(BigInteger  val)  返回其值为(this+  val)的BigInteger。

    BigInteger

    and(BigInteger  val)   返回其值为(this&  val)的BigInteger。

    BigInteger

    andNot(BigInteger  val)   返回其值为(this&~  val)的BigInteger。

    int

    bitCount()   返回此BigInteger的二进制补码表示形式中与符号不同的位的数量。

    int

    bitLength()   返回此BigInteger的最小的二进制补码表示形式的位数,不包括符号位。

    BigInteger

    clearBit(int n)   返回其值与清除了指定位的此BigInteger等效的BigInteger。

    int

    compareTo(BigInteger val)   将此BigInteger与指定的BigInteger进行比较。

    BigInteger

    divide(BigInteger val)   返回其值为(this/val)的BigInteger。

    BigInteger[]

    divideAndRemainder(BigInteger val)   返回包含(this/  val)后跟(this%  val)的两个BigInteger的数组。

    double

    double  value()   将此BigInteger转换为double。

    boolean

    equals(Object x)   比较此BigInteger与指定的Object的相等性。

    BigInteger

    flipBit(int  n)   返回其值与对此BigInteger进行指定位翻转后的值等效的BigInteger。

    float

    float  value()   将此BigInteger转换为float。

    BigInteger

    gcd(BigInteger val)   返回一个BigInteger,其值是abs(this)和abs(  val)的最大公约数。

    int

    getLowestSetBit()   返回此BigInteger最右端(最低位)1比特的索引(即从此字节的右端开始到本字节中最右端1比特之间的0比特的位数)。

    int

    hashCode()   返回此BigInteger的哈希码。

    int

    int  value()   将此BigInteger转换为int。

    boolean

    isProbablePrime(int certainty)   如果此BigInteger可能为素数,则返回true,如果它一定为合数,则返回false。

    long

    long  value()   将此BigInteger转换为long。

    BigInteger

    max(BigInteger  val)   返回此BigInteger和  val的最大值。

    BigInteger

    min(BigInteger  val)   返回此BigInteger和  val的最小值。

    BigInteger

    mod(BigInteger  m)   返回其值为(thismodm)的BigInteger。

    BigInteger

    modInverse(BigInteger  m)   返回其值为(this-1modm)的BigInteger。

    BigInteger

    modPow(BigInteger  exponent,BigInteger  m)   返回其值为(thisexponentmodm)的BigInteger。

    BigInteger

    multiply(BigInteger  val)   返回其值为(this*  val)的BigInteger。

    BigInteger

    negate()   返回其值是(-this)的BigInteger。

    BigInteger

    nextProbablePrime()   返回大于此BigInteger的可能为素数的第一个整数。

    BigInteger

    not()   返回其值为(~this)的BigInteger。

    BigInteger

    or(BigInteger  val)   返回其值为(this|  val)的BigInteger。

    BigInteger

    pow(intexponent)   返回其值为(thisexponent)的BigInteger。

    Static  BigInteger

    probablePrime(int  bitLength,Random  rnd)   返回有可能是素数的、具有指定长度的正BigInteger。

    BigInteger

    remainder(BigInteger  val)   返回其值为(this% val)的BigInteger。

    BigInteger

    setBit(int  n)   返回其值与设置了指定位的此BigInteger等效的BigInteger。

    BigInteger

    shiftLeft(int  n)   返回其值为(this<<n)的BigInteger。

    BigInteger

    shiftRight(int  n)   返回其值为(this>>n)的BigInteger。

    int

    signum()   返回此BigInteger的正负号函数。

    BigInteger

    subtract(BigInteger  val)   返回其值为(this-  val)的BigInteger。

    boolean

    testBit(intn)   当且仅当设置了指定的位时,返回true。

    byte[]

    toByteArray()   返回一个字节数组,该数组包含此BigInteger的二进制补码表示形式。

    String

    toString()   返回此BigInteger的十进制字符串表示形式。

    String

    toString(intradix)   返回此BigInteger的给定基数的字符串表示形式。

    staticBigInteger

    valueOf(long val)   返回其值等于指定long的值的BigInteger。

    BigInteger

    xor(BigInteger  val)   返回其值为(this^  val)的BigInteger。

     






     


  • 相关阅读:
    [LeetCode] Dungeon Game
    [LeetCode] Maximal Rectangle
    [LeetCode] Scramble String -- 三维动态规划的范例
    [LeetCode] Palindrome Partitioning II
    [LeetCode] Palindrome Partitioning
    [LeetCode] Interleaving String
    [LeetCode] Longest Valid Parentheses -- 挂动态规划羊头卖stack的狗肉
    JPA将查询结果转换为DTO对象
    EOS签名R值过大导致报错"is_canonical( c ): signature is not canonical"
    比特币中P2SH(pay-to-script-hash)多重签名的锁定脚本和解锁脚本
  • 原文地址:https://www.cnblogs.com/zswbky/p/6792859.html
Copyright © 2011-2022 走看看