// 新建一个值为777的大整数的对象
BigInteger a = new BigInteger("777"); //参数可以是字符串
BigInteger b = BigInteger.valueOf(77); //参数可以使int,long
System.out.println("a = "+a);
System.out.println("b = "+b);
//大整数的四则运算
BigInteger c = a.add(b);
System.out.println("a + b = "+c);
c = a.subtract(b);
System.out.println("a - b = "+c);
c = a.divide(b);
System.out.println("a / b = "+c);
c = a.multiply(b);
System.out.println("a * b = "+c);
//大整数比较大小
boolean d = a.equals(b); //如果a和b相等返回true 否则返回false
System.out.println("a > b ? "+d);
int e = a.compareTo(b); //如果a小于b返回-1,等于返回0,大于返回1
System.out.println("a > b = "+e);
//常用的方法
//求余数
c = a.mod(b);
System.out.println("a % b = "+c);
//求最大公约数
c = a.gcd(b);
System.out.println("gcd(a,b) = "+c);
//求最大值
c = a.max(b);
System.out.println("max(a,b) = "+c);
//求最小值
c = a.min(b);
System.out.println("min(a,b) = "+c);
//BigInteger中的常数
System.out.println("大整数0 = "+BigInteger.ZERO);
System.out.println("大整数1 = "+BigInteger.ONE);
System.out.println("大整数10 = "+BigInteger.TEN);
//求大整数的位数
int f = a.toString().length();
System.out.println("a的位数 = "+f);
//返回大整数的符号 正数1 0 负数-1
System.out.println("a的符号 "+a.signum());
上述的输出:
a = 777
b = 77
a + b = 854
a - b = 700
a / b = 10
a * b = 59829
a > b ? false
a > b = 1
a % b = 7
gcd(a,b) = 7
max(a,b) = 777
min(a,b) = 77
大整数0 = 0
大整数1 = 1
大整数10 = 10
a的位数 = 3
a的符号 1
1.输入:不断地输入大整数直至文件尾
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
System.out.println("输入连续的大数:");
while(cin.hasNext())//等同于 !=EOF
{
BigInteger h;
h = cin.nextBigInteger(); //读入一个BigInteger
System.out.println(h);
}
System.out.println("输入结束");
}
}
2.大整数加法:求两个不超过200位的非负整数的和。
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
BigInteger a,b;
Scanner cin = new Scanner(System.in);
a = cin.nextBigInteger();
b = cin.nextBigInteger();
System.out.println(a.add(b));
}
}
3.大数阶乘:给一个n,计算n的阶乘,但是n很可能比较大
分析:可以从1开始打一个阶乘表,这样的话当有输入的时候我们就可以用O(1)的复杂度解决它
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
BigInteger f[] = new BigInteger[5500];
f[0] = f[1] = BigInteger.ONE;
for (int i = 2; i <= 5000; ++i) {
f[i] = f[i - 1].multiply(BigInteger.valueOf(i));
}
Scanner cin = new Scanner(System.in);
while (cin.hasNext()) {
int m = cin.nextInt();
System.out.println(f[m]);
}
}
}
4.大菲波数:计算第n项Fibonacci(斐波那契数列)数值,n可能比较大
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
BigInteger[] nums = new BigInteger[1010];
nums[1] = new BigInteger("1");
nums[2] = new BigInteger("1");
for(int i = 3; i <= 1000; i++)
nums[i] = nums[i - 1].add(nums[i - 2]);
int T = cin.nextInt();
while(T > 0)
{
T--;
int n = cin.nextInt();
System.out.println(nums[n]);
}
}
}
5.A/B:求(A/B)%9973,但由于A很大,我们只给出n(n=A%9973)(我们给定的A必能被B整除,且gcd(B,9973) = 1)。即要输入n和B,输出(A/B)%9973。
分析:因为a一定能被b整除,所以a一定是b的倍数,于是我们让a = b * i(i= 1,2,3···)直到a Mod b == n;就可以得到大整数a,就可以用题目中给的公式将正确答案做出来 。
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int T = cin.nextInt();
while(T > 0)
{
T--;
BigInteger a = cin.nextBigInteger();
BigInteger b = cin.nextBigInteger();
BigInteger d = new BigInteger("9973");
BigInteger z = new BigInteger("0");
for(BigInteger i = new BigInteger("1"); ; i = i.add(new BigInteger("1")))
{
BigInteger c = b.multiply(i);
if(c.mod(d).compareTo(a) == 0)
{
System.out.println(i.mod(d));
break;
}
}
}
}
}