算法拾遗[5]——Java大数

比较重要的几条是:

  • add, subtract, multiply, divide, mod, remainder 加减乘除取模取余—注意取模中 \(b\) 必须为正!
  • a.compareTo(b) \(a=b\) 返回 \(0\), 否则返回 \(a>b\) 的值.
  • a.toString(b)\(a\) 转换为 \(b\) 进制字符串.

另外注意 Scanner 的写法:

  • 输入接口: Scanner cin = new Scanner(System.in);
  • EOF写法: while(cin.hasNext()) {}
  • 输入:BigInteger a = cin.nextBigInteger();

以及变量的声明(注意 new):

  • 单个变量: BigInteger a = new BigInteger("0");
  • 声明数组: BigInteger a[] = new BigInteger[size];
  • 静态方法: BigInteger a = BigInteger.valueOf(x);

完整版本如下:

来源: https://blog.csdn.net/qq644262163/article/details/53116713

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import java.util.*;  
import java.math.*;

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 a = new BigInteger("3");
// 将radix进制的字符串转化为BigInteger
// public BigInteger(String val, int radix)
BigInteger a = new BigInteger("3", 2);
// 将整数赋给BigInteger
BigInteger a = BigInteger.valueOf(100);

// 常量
a = BigInteger.ZERO;
a = BigInteger.ONE;
a = BigInteger.TEN;

// 值等于val的值
// public static BigInteger valueOf(long val)
BigInteger.valueOf(10);

a.add(b);
a.subtract(b);
a.multiply(b);
a.divide(b);
// 取模a%b b需大于0 5mod3=2 -5mod3=1
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);
// 乘方
// public BigInteger pow(int exponent)
a.pow(3);
// a模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) {
}

}
}