在用C或者C++处理大数时感觉非常麻烦,但是在JAVA中有两个类BigInteger和BigDecimal分别表示大整数类和大浮点数类,
两个类的对象能表示最大范围理论上能够表示无线大的数,只要计算机内存足够大。 这两个类都在java.math.*包中,因此每次必须在开头处引用该包。


Ⅰ基本函数:

1.valueOf(parament); 将参数转换为制定的类型
比如 int a=3;
BigInteger b=BigInteger.valueOf(a);

则b=3;

String s=”12345”;
BigInteger c=BigInteger.valueOf(s);

则c=12345; 
                                                                                    

2.add(); 大整数相加
BigInteger a=new BigInteger(“23”);
BigInteger b=new BigInteger(“34”);
a.add(b);

3.subtract(); 相减

4.multiply(); 相乘

5.divide(); 相除取整

6.remainder(); 取余

7.pow(); a.pow(b)=a^b

8.gcd(); 最大公约数

9.abs(); 绝对值

10.negate(); 取反数

11.mod(); a.mod(b)=a%b=a.remainder(b);

12.max(); min();

13.public int comareTo();

14.boolean equals(); 是否相等

15.BigInteger构造函数:

一般用到以下两种:
BigInteger(String val);
将指定字符串转换为十进制表示形式;

BigInteger(String val,int radix);
将指定基数的 BigInteger 的字符串表示形式转换为 BigInteger

Ⅱ.基本常量:

A=BigInteger.ONE 1
B=BigInteger.TEN 10
C=BigInteger.ZERO 0

Ⅲ.基本操作
1. 读入:
用Scanner类定义对象进行控制台读入,Scanner类在java.util.*包中

Scanner cin=new Scanner(System.in);// 读入
while(cin.hasNext()) //等同于!=EOF
{
   int n;
   BigInteger m;
   n=cin.nextInt(); //读入一个int;
   m=cin.nextBigInteger();//读入一个BigInteger;
   System.out.print(m.toString());

}

Ⅳ.运用

四则预算:

import java.util.Scanner;import java.math.*;import java.text.*;public class Main {public static void main(String args[]){ Scanner cin = new Scanner ( System.in ); BigInteger a,b; int c; char op; String s; while(cin.hasNext() ) { a = cin.nextBigInteger(); s = cin.next(); op = s.charAt(0); if( op == '+') { b = cin.nextBigInteger(); System.out.println(a.add(b)); } else if( op == '-') { b = cin.nextBigInteger(); System.out.println(a.subtract(b)); } else if( op == '*') { b = cin.nextBigInteger(); System.out.println(a.multiply(b)); } else { BigDecimal a1,b1,eps; String s1,s2,temp; s1 = a.toString(); a1 = new BigDecimal(s1); b = cin.nextBigInteger(); s2 = b.toString(); b1 = new BigDecimal(s2); c = cin.nextInt(); eps = a1.divide(b1,c,4); System.out.print( a.divide(b) + " " + a.mod(b) + " "); if( c != 0) { temp = "0."; for(int i = 0; i < c; i ++) temp += "0"; DecimalFormat gd = new DecimalFormat(temp); System.out.println(gd.format(eps)); } else System.out.println(eps); } } }}例子:求一个n元素集合中k个元素的组合数量:C(n,k) = n!/(k! * (n-k)!)import java.math.BigInteger; import java.util.*;import java.io.*; public class FactorialBigIntegerTest { public static void main(String[] args) throws Exception { Scanner cin=new Scanner(System.in); int T = cin.nextInt(); for(int i = 0;i < T;i++){ int n=cin.nextInt(),k=cin.nextInt(); BigInteger nf= new BigInteger(""+factorial(n)); BigInteger kf= new BigInteger(""+factorial(k)); BigInteger nk = new BigInteger(""+factorial(n-k)); BigInteger n1 = new BigInteger(""+nf.divide(kf)); System.out.println(n1.divide(nk)); } } private static final BigInteger factorial(int n){ BigInteger bigInteger=new BigInteger(""+n); if(n==0) return BigInteger.ONE; else return factorial(n-1).multiply(bigInteger); } } 输入 2000 50运行结果:

19961695734279236211032903597262781251004330223561192760088208846391036329722665786642280715824928160

 

例子:

poj2305

大数的进制转换

 

String st = Integer.toString(num, base); // 把num当做10进制的数转成base进制的st(base <= 35).
int num = Integer.parseInt(st, base); // 把st当做base进制,转成10进制的int(parseInt有两个参数,第一个为要转的字符串,
                                      // 第二个为说明是什么进制).
BigInter m = new BigInteger(st, base); // st是字符串,base是st的进制.

1.如果要将一个大数以2进制形式读入 可以使用cin.nextBigInteger(2);
当然也可以使用其他进制方式读入;

2.如果要将一个大数转换成其他进制形式的字符串 使用cin.toString(2);//将它转换成2进制表示的字符串

 

关键代码:

Scanner cin=new Scanner(System.in);BigInteger p,m,ans;

p=cin.nextBigInteger(b);//从命令行以b进制读大数m=cin.nextBigInteger(b);ans=p.mod(m);//p%m