学习 BigInteger

以下是摘抄与其他人的:  

JAVA之BigInteger

 

 

Java来处理高精度问题,相信对很多ACMer来说都是一件很happy的事,简单易懂。用Java刷了一些题,感觉Java还不错,在处理高精度和进制转换中,调用库函数的来处理。下面是写的一些Java中一些基本的函数的及其……

头文件:import java.io.*;

import java.util.*;

import java.math.*;

读入: Scanner cin = Scanner (System.in);

while(cin.hasNext())//等价于!=EOF

n=cin.nextInt();//读入一个int型的数

n=cin.nextBigInteger();//读入一个大整数

输出: System.out.print(n);//打印n

System.out.println();//换行

System.out.printf("%d\n",n);//也可以类似c++里的输出方式

定义: int i,j,k,a[];

a = new int[100];

BigInteger n,m;

BigDecimal n;

String s;

数据类型:

数据类型 类型名 位长 取值范围 默认值

布尔型 boolean 1 true,false false

字节型 byte 8 -128-127 0

字符型 char 16 ‘\u000’-\uffff ‘\u0000’

短整型 short 16 -32768-32767 0

整型 int 32 -2147483648,2147483647 0

长整型 long 64 -9.22E18,9.22E18 0

浮点型 float 32 1.4E-45-3.4028E+38 0.0

双精度型 double 64 4.9E-324,1.7977E+308 0.0

这里特别要提出出的两种类型:

BigInteger 任意大的整数,原则上是,只要你的计算机的内存足够大,可以有无限位的

BigInteger 任意大的实数,可以处理小数精度问题。

BigInteger中一些常见的函数:

A=BigInteger.ONE

B=BigInteger.TEN

C=BigInteger.ZERO

一些常见的数的赋初值。将int型的数赋值给BigInteger,BigInteger.valueOf(k);

基本的函数:

valueOf:赋初值

add:+ a.add(b);

subtract:-

multiply:*

divide:/

remainder:this % val

divideAndRemainder:a[0]=this / val; a[1]=this % val

pow:a.pow(b)=a^b

gcd,abs:公约数,绝对值

negate:取负数

signum:符号函数

mod:a.mod(b)=a%b;

shiftLeft:左移,this << n ,this*2^n;

shiftRight:右移,this >> n,this/2^n;

and:等同于c++的&&,且;

or:||,或;

xor:异或,BigInteger xor(BigInteger val),this^val

not:!,非;

bitLength:返回该数的最小二进制补码表示的位的个数,即 *不包括* 符号位 (ceil(log2(this <0 ? -this : this + 1)))。对正数来说,这等价于普通二进制表示的位的个数。

bitCount:返回该数的二进制补码表示中不包扩符号位在内的位的个数。该方法在 BigIntegers 之上实现位向量风格的集合时很有用。

isProbablePrime:如果该 BigInteger 可能是素数,则返回 true ;如果它很明确是一个合数,则返回 false 。 参数 certainty 是对调用者愿意忍受的不确定性的度量:如果该数是素数的概率超过了 1 - 1/2**certainty方法,则该方法返回 true 。执行时间正比于参数确定性的值。

compareTo:根据该数值是小于、等于、或大于 val 返回 -1、0 或 1;

equals:判断两数是否相等,也可以用compareTo来代替;

min,max:取两个数的较小、大者;

intValue,longValue,floatValue,doublue:把该数转换为该类型的数的值。

 

 

 

 

今天参考课本写了一个关于二进制与十进制转换的程序,程序算法不难,但写完后测试发现不论是二转十还是十转二,对于大于21亿即超过整数范围的数不能很好的转换。都会变成0.
参考书籍发现使用使用BigInteger可以解决这个问题。
于是查找了下JDK,然后测试几次终于写成功了!
使用心得如下:

1,BigInteger属于java.math.BigInteger,因此在每次使用前都要import 这个类。偶开始就忘记import了,于是总提示找不到提示符。

2,其构造方法有很多,但现在偶用到的有: BigInteger(String val)
将 BigInteger 的十进制字符串表示形式转换为 BigInteger。
BigInteger(String val, int radix)
将指定基数的 BigInteger 的字符串表示形式转换为 BigInteger。
如要将int型的2转换为BigInteger型,要写为BigInteger two=new BigInteger("2"); //注意2双引号不能省略

3,BigInteger类模拟了所有的int型数学操作,如add()==“+”,divide()==“-”等,但注意其内容进行数学运算时不能直接使用数学运算符进行运算,必须使用其内部方法。而且其操作数也必须为BigInteger型。
如:two.add(2)就是一种错误的操作,因为2没有变为BigInteger型。

4,当要把计算结果输出时应该使用.toString方法将其转换为10进制的字符串,详细说明如下:
String toString()
返回此 BigInteger 的十进制字符串表示形式。
输出方法:System.out.print(two.toString());

5,另外说明三个个用到的函数。 BigInteger remainder(BigInteger val)
返回其值为 (this % val) 的 BigInteger。
BigInteger negate()
返回其值是 (-this) 的 BigInteger。
int compareTo(BigInteger val)
将此 BigInteger 与指定的 BigInteger 进行比较。
remainder用来求余数。
negate将操作数变为相反数。
compare的详解如下:

compareTo
public int compareTo(BigInteger val)将此 BigInteger 与指定的 BigInteger 进行比较。对于针对六个布尔比较运算符 (<, ==, >, >=, !=, <=) 中的每一个运算符的各个方法,优先提供此方法。执行这些比较的建议语句是:(x.compareTo(y) <op> 0),其中 <op> 是六个比较运算符之一。
指定者:
接口 Comparable<BigInteger> 中的 compareTo
参数:
val - 将此 BigInteger 与之比较的 BigInteger。
返回:

将BigInteger的数转为2进制:

public class TestChange {
public static void main(String[] args) {
System.out.println(change("3",10,2));
}
//num 要转换的数 from源数的进制 to要转换成的进制
private static String change(String num,int from, int to){
return new java.math.BigInteger(num, from).toString(to);
}
}

 

其中以实现 Fibonacci数列 进行练习

问题描述

Fibonacci数列的递推公式为:Fn=Fn-1+Fn-2,其中F1=F2=1。

当n比较大时,Fn也非常大,现在我们想知道,Fn除以10007的余数是多少。

输入格式
输入包含一个整数n。
输出格式
输出一行,包含一个整数,表示Fn除以10007的余数。

说明:在本题中,答案是要求Fn除以10007的余数,因此我们只要能算出这个余数即可,而不需要先计算出Fn的准确值,再将计算的结果除以10007取余数,直接计算余数往往比先算出原数再取余简单。

样例输入
10
样例输出
55
样例输入
22
样例输出
7704
数据规模与约定
1 <= n <= 1,000,000。
 
解:一下是用的数组方式实现

import java.util.Scanner;
import java.math.BigInteger;

public class BigIntegerFibnaccui {
public static void main(String[] args) {

Scanner sc= new Scanner(System.in);
int num = sc.nextInt();

int[] resultInt = new int[num];
resultInt[0] = 1;
resultInt[1] = 1;

long[] resultLong = new long[num];
resultLong[0] = 1L;
resultLong[1] = 1L;

BigInteger[] resultBigInteger = new BigInteger[num];
resultBigInteger[0] = BigInteger.ONE;
resultBigInteger[1] = BigInteger.ONE;
for (int i = 2; i < num; i++) {
resultInt[i] = resultInt[i - 1] + resultInt[i - 2];
resultLong[i] = resultLong[i - 1] + resultLong[i - 2];
resultBigInteger[i] = resultBigInteger[i - 1].add(resultBigInteger[i - 2]);
}

System.out.println("--------------------output int---------------------");
for (int i = 0; i < resultInt.length; i++) {
System.out.println("resultInt[" + i + "] = "+ resultInt[i]);
}

System.out.println("--------------------output long---------------------");
for (int i = 0; i < resultLong.length; i++) {
System.out.println("resultLong[" + i + "] = "+ resultLong[i]);
}

System.out.println("--------------------output bigInteger---------------------");
for (int i = 0; i < resultBigInteger.length; i++) {
System.out.println("resultBigInteger[" + i + "] = "+ resultBigInteger[i]);
}
}
}

   心得:

     初看这道题很简单,我们只需要设定一个数组,然后往里面放入对应的值,除了前两个数字位,后面每个数字位都是前两个数字位的和。刚开始我用了int数组来存放这组数,发现根本放不下去,到了第47位数的时候就存在数值溢出了,然后我换为long型数组,同样放不下去,到了第93位数的时候也存在数值溢出了。这样,只能用BigInteger类了,这是java提供的可以存储任意精度的整数的类,用它我们不需要考虑数值溢出的错误。即使我们求第1000个数值也没有什么问题。不过,它的数值运算必须以方法调用方式取代运算符方式来实现,由于这么做复杂了许多,运算速度会比较慢,所以在确定数值不会溢出的情况下,是不推荐用这种型式来进行数值存储和运算的。

结果:

100
--------------------output int---------------------
resultInt[0] = 1
resultInt[1] = 1
resultInt[2] = 2
resultInt[3] = 3
resultInt[4] = 5
resultInt[5] = 8
resultInt[6] = 13
resultInt[7] = 21
resultInt[8] = 34
resultInt[9] = 55
resultInt[10] = 89
resultInt[11] = 144
resultInt[12] = 233
resultInt[13] = 377
resultInt[14] = 610
resultInt[15] = 987
resultInt[16] = 1597
resultInt[17] = 2584
resultInt[18] = 4181
resultInt[19] = 6765
resultInt[20] = 10946
resultInt[21] = 17711
resultInt[22] = 28657
resultInt[23] = 46368
resultInt[24] = 75025
resultInt[25] = 121393
resultInt[26] = 196418
resultInt[27] = 317811
resultInt[28] = 514229
resultInt[29] = 832040
resultInt[30] = 1346269
resultInt[31] = 2178309
resultInt[32] = 3524578
resultInt[33] = 5702887
resultInt[34] = 9227465
resultInt[35] = 14930352
resultInt[36] = 24157817
resultInt[37] = 39088169
resultInt[38] = 63245986
resultInt[39] = 102334155
resultInt[40] = 165580141
resultInt[41] = 267914296
resultInt[42] = 433494437
resultInt[43] = 701408733
resultInt[44] = 1134903170
resultInt[45] = 1836311903
resultInt[46] = -1323752223
resultInt[47] = 512559680
resultInt[48] = -811192543
resultInt[49] = -298632863
resultInt[50] = -1109825406
resultInt[51] = -1408458269
resultInt[52] = 1776683621
resultInt[53] = 368225352
resultInt[54] = 2144908973
resultInt[55] = -1781832971
resultInt[56] = 363076002
resultInt[57] = -1418756969
resultInt[58] = -1055680967
resultInt[59] = 1820529360
resultInt[60] = 764848393
resultInt[61] = -1709589543
resultInt[62] = -944741150
resultInt[63] = 1640636603
resultInt[64] = 695895453
resultInt[65] = -1958435240
resultInt[66] = -1262539787
resultInt[67] = 1073992269
resultInt[68] = -188547518
resultInt[69] = 885444751
resultInt[70] = 696897233
resultInt[71] = 1582341984
resultInt[72] = -2015728079
resultInt[73] = -433386095
resultInt[74] = 1845853122
resultInt[75] = 1412467027
resultInt[76] = -1036647147
resultInt[77] = 375819880
resultInt[78] = -660827267
resultInt[79] = -285007387
resultInt[80] = -945834654
resultInt[81] = -1230842041
resultInt[82] = 2118290601
resultInt[83] = 887448560
resultInt[84] = -1289228135
resultInt[85] = -401779575
resultInt[86] = -1691007710
resultInt[87] = -2092787285
resultInt[88] = 511172301
resultInt[89] = -1581614984
resultInt[90] = -1070442683
resultInt[91] = 1642909629
resultInt[92] = 572466946
resultInt[93] = -2079590721
resultInt[94] = -1507123775
resultInt[95] = 708252800
resultInt[96] = -798870975
resultInt[97] = -90618175
resultInt[98] = -889489150
resultInt[99] = -980107325
--------------------output long---------------------

resultLong[0] = 1
resultLong[1] = 1
resultLong[2] = 2
resultLong[3] = 3
resultLong[4] = 5
resultLong[5] = 8
resultLong[6] = 13
resultLong[7] = 21
resultLong[8] = 34
resultLong[9] = 55
resultLong[10] = 89
resultLong[11] = 144
resultLong[12] = 233
resultLong[13] = 377
resultLong[14] = 610
resultLong[15] = 987
resultLong[16] = 1597
resultLong[17] = 2584
resultLong[18] = 4181
resultLong[19] = 6765
resultLong[20] = 10946
resultLong[21] = 17711
resultLong[22] = 28657
resultLong[23] = 46368
resultLong[24] = 75025
resultLong[25] = 121393
resultLong[26] = 196418
resultLong[27] = 317811
resultLong[28] = 514229
resultLong[29] = 832040
resultLong[30] = 1346269
resultLong[31] = 2178309
resultLong[32] = 3524578
resultLong[33] = 5702887
resultLong[34] = 9227465
resultLong[35] = 14930352
resultLong[36] = 24157817
resultLong[37] = 39088169
resultLong[38] = 63245986
resultLong[39] = 102334155
resultLong[40] = 165580141
resultLong[41] = 267914296
resultLong[42] = 433494437
resultLong[43] = 701408733
resultLong[44] = 1134903170
resultLong[45] = 1836311903
resultLong[46] = 2971215073
resultLong[47] = 4807526976
resultLong[48] = 7778742049
resultLong[49] = 12586269025
resultLong[50] = 20365011074
resultLong[51] = 32951280099
resultLong[52] = 53316291173
resultLong[53] = 86267571272
resultLong[54] = 139583862445
resultLong[55] = 225851433717
resultLong[56] = 365435296162
resultLong[57] = 591286729879
resultLong[58] = 956722026041
resultLong[59] = 1548008755920
resultLong[60] = 2504730781961
resultLong[61] = 4052739537881
resultLong[62] = 6557470319842
resultLong[63] = 10610209857723
resultLong[64] = 17167680177565
resultLong[65] = 27777890035288
resultLong[66] = 44945570212853
resultLong[67] = 72723460248141
resultLong[68] = 117669030460994
resultLong[69] = 190392490709135
resultLong[70] = 308061521170129
resultLong[71] = 498454011879264
resultLong[72] = 806515533049393
resultLong[73] = 1304969544928657
resultLong[74] = 2111485077978050
resultLong[75] = 3416454622906707
resultLong[76] = 5527939700884757
resultLong[77] = 8944394323791464
resultLong[78] = 14472334024676221
resultLong[79] = 23416728348467685
resultLong[80] = 37889062373143906
resultLong[81] = 61305790721611591
resultLong[82] = 99194853094755497
resultLong[83] = 160500643816367088
resultLong[84] = 259695496911122585
resultLong[85] = 420196140727489673
resultLong[86] = 679891637638612258
resultLong[87] = 1100087778366101931
resultLong[88] = 1779979416004714189
resultLong[89] = 2880067194370816120
resultLong[90] = 4660046610375530309
resultLong[91] = 7540113804746346429
resultLong[92] = -6246583658587674878
resultLong[93] = 1293530146158671551
resultLong[94] = -4953053512429003327
resultLong[95] = -3659523366270331776
resultLong[96] = -8612576878699335103
resultLong[97] = 6174643828739884737
resultLong[98] = -2437933049959450366
resultLong[99] = 3736710778780434371

--------------------output bigInteger---------------------

resultBigInteger[0] = 1
resultBigInteger[1] = 1
resultBigInteger[2] = 2
resultBigInteger[3] = 3
resultBigInteger[4] = 5
resultBigInteger[5] = 8
resultBigInteger[6] = 13
resultBigInteger[7] = 21
resultBigInteger[8] = 34
resultBigInteger[9] = 55
resultBigInteger[10] = 89
resultBigInteger[11] = 144
resultBigInteger[12] = 233
resultBigInteger[13] = 377
resultBigInteger[14] = 610
resultBigInteger[15] = 987
resultBigInteger[16] = 1597
resultBigInteger[17] = 2584
resultBigInteger[18] = 4181
resultBigInteger[19] = 6765
resultBigInteger[20] = 10946
resultBigInteger[21] = 17711
resultBigInteger[22] = 28657
resultBigInteger[23] = 46368
resultBigInteger[24] = 75025
resultBigInteger[25] = 121393
resultBigInteger[26] = 196418
resultBigInteger[27] = 317811
resultBigInteger[28] = 514229
resultBigInteger[29] = 832040
resultBigInteger[30] = 1346269
resultBigInteger[31] = 2178309
resultBigInteger[32] = 3524578
resultBigInteger[33] = 5702887
resultBigInteger[34] = 9227465
resultBigInteger[35] = 14930352
resultBigInteger[36] = 24157817
resultBigInteger[37] = 39088169
resultBigInteger[38] = 63245986
resultBigInteger[39] = 102334155
resultBigInteger[40] = 165580141
resultBigInteger[41] = 267914296
resultBigInteger[42] = 433494437
resultBigInteger[43] = 701408733
resultBigInteger[44] = 1134903170
resultBigInteger[45] = 1836311903
resultBigInteger[46] = 2971215073
resultBigInteger[47] = 4807526976
resultBigInteger[48] = 7778742049
resultBigInteger[49] = 12586269025
resultBigInteger[50] = 20365011074
resultBigInteger[51] = 32951280099
resultBigInteger[52] = 53316291173
resultBigInteger[53] = 86267571272
resultBigInteger[54] = 139583862445
resultBigInteger[55] = 225851433717
resultBigInteger[56] = 365435296162
resultBigInteger[57] = 591286729879
resultBigInteger[58] = 956722026041
resultBigInteger[59] = 1548008755920
resultBigInteger[60] = 2504730781961
resultBigInteger[61] = 4052739537881
resultBigInteger[62] = 6557470319842
resultBigInteger[63] = 10610209857723
resultBigInteger[64] = 17167680177565
resultBigInteger[65] = 27777890035288
resultBigInteger[66] = 44945570212853
resultBigInteger[67] = 72723460248141
resultBigInteger[68] = 117669030460994
resultBigInteger[69] = 190392490709135
resultBigInteger[70] = 308061521170129
resultBigInteger[71] = 498454011879264
resultBigInteger[72] = 806515533049393
resultBigInteger[73] = 1304969544928657
resultBigInteger[74] = 2111485077978050
resultBigInteger[75] = 3416454622906707
resultBigInteger[76] = 5527939700884757
resultBigInteger[77] = 8944394323791464
resultBigInteger[78] = 14472334024676221
resultBigInteger[79] = 23416728348467685
resultBigInteger[80] = 37889062373143906
resultBigInteger[81] = 61305790721611591
resultBigInteger[82] = 99194853094755497
resultBigInteger[83] = 160500643816367088
resultBigInteger[84] = 259695496911122585
resultBigInteger[85] = 420196140727489673
resultBigInteger[86] = 679891637638612258
resultBigInteger[87] = 1100087778366101931
resultBigInteger[88] = 1779979416004714189
resultBigInteger[89] = 2880067194370816120
resultBigInteger[90] = 4660046610375530309
resultBigInteger[91] = 7540113804746346429
resultBigInteger[92] = 12200160415121876738
resultBigInteger[93] = 19740274219868223167
resultBigInteger[94] = 31940434634990099905
resultBigInteger[95] = 51680708854858323072
resultBigInteger[96] = 83621143489848422977
resultBigInteger[97] = 135301852344706746049
resultBigInteger[98] = 218922995834555169026
resultBigInteger[99] = 354224848179261915075
请按任意键继续. . .

 

如上可以见:用BigInteger不会出现负数形式!

posted @ 2016-02-13 19:53  dy9776  阅读(951)  评论(0编辑  收藏  举报