﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-From A Start,As An Acmer-随笔分类-他山之石</title><link>http://www.cppblog.com/aswmtjdsj/category/17606.html</link><description>My Way to Final</description><language>zh-cn</language><lastBuildDate>Sat, 20 Aug 2011 12:39:06 GMT</lastBuildDate><pubDate>Sat, 20 Aug 2011 12:39:06 GMT</pubDate><ttl>60</ttl><item><title>JAVA之BigInteger(转)【转】【很好用啊】</title><link>http://www.cppblog.com/aswmtjdsj/archive/2011/08/20/153973.html</link><dc:creator>BUPT-[aswmtjdsj] @ Penalty</dc:creator><author>BUPT-[aswmtjdsj] @ Penalty</author><pubDate>Sat, 20 Aug 2011 11:36:00 GMT</pubDate><guid>http://www.cppblog.com/aswmtjdsj/archive/2011/08/20/153973.html</guid><wfw:comment>http://www.cppblog.com/aswmtjdsj/comments/153973.html</wfw:comment><comments>http://www.cppblog.com/aswmtjdsj/archive/2011/08/20/153973.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/aswmtjdsj/comments/commentRss/153973.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/aswmtjdsj/services/trackbacks/153973.html</trackback:ping><description><![CDATA[<div><span style="font-family: Arial; line-height: 18px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; "><p align="left" style="line-height: normal; ">用<a href="http://hi.baidu.com/buptyoyo/blog/item/:;" target="_self" style="color: #004f61; font-size: 12px; font-family: Verdana, Arial, Helvetica, sans-serif; font-weight: normal; text-decoration: none; line-height: normal; "><u style="line-height: normal; "><strong style="line-height: normal; ">Java</strong></u></a>来处理高精度问题，相信对很多ACMer来说都是一件很happy的事，简单易懂。用Java刷了一些题，感觉Java还不错，在处理高精度和进制转换中，调用库函数的来处理。下面是写的一些Java中一些基本的函数的及其&#8230;&#8230;</p><p align="left" style="line-height: normal; ">头文件：import java.io.*;</p><p align="left" style="line-height: normal; "><wbr style="line-height: normal; ">&nbsp;<wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; ">import java.util.*;</p><p align="left" style="line-height: normal; "><wbr style="line-height: normal; ">&nbsp;<wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; ">import java.math.*;</p><p align="left" style="line-height: normal; ">读入：&nbsp;<wbr style="line-height: normal; ">Scanner cin = Scanner (System.in);</p><p align="left" style="line-height: normal; "><wbr style="line-height: normal; ">&nbsp;<wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; ">while(cin.hasNext())//等价于!=EOF</p><p align="left" style="line-height: normal; "><wbr style="line-height: normal; ">&nbsp;<wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; ">n=cin.nextInt();//读入一个int型的数</p><p align="left" style="line-height: normal; "><wbr style="line-height: normal; ">&nbsp;<wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; ">n=cin.nextBigInteger();//读入一个大整数</p><p align="left" style="line-height: normal; ">输出：&nbsp;<wbr style="line-height: normal; ">System.out.print(n);//打印n</p><p align="left" style="line-height: normal; "><wbr style="line-height: normal; ">&nbsp;<wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; ">System.out.println();//换行</p><p align="left" style="line-height: normal; "><wbr style="line-height: normal; ">&nbsp;<wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; ">System.out.printf("%d\n",n);//也可以类似c++里的输出方式</p><p align="left" style="line-height: normal; ">定义：&nbsp;<wbr style="line-height: normal; ">int i,j,k,a[];</p><p align="left" style="line-height: normal; "><wbr style="line-height: normal; ">&nbsp;<wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; ">a = new int[100];</p><p align="left" style="line-height: normal; "><wbr style="line-height: normal; ">&nbsp;<wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; ">BigInteger n,m;</p><p align="left" style="line-height: normal; "><wbr style="line-height: normal; ">&nbsp;<wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; ">BigDecimal n;</p><p align="left" style="line-height: normal; "><wbr style="line-height: normal; ">&nbsp;<wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; ">String s;</p><p align="left" style="line-height: normal; ">数据类型：</p><p align="left" style="line-height: normal; ">数据类型&nbsp;<wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; ">类型名&nbsp;<wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; ">位长&nbsp;<wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; ">取值范围&nbsp;<wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; ">默认值</p><p align="left" style="line-height: normal; ">布尔型&nbsp;<wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; ">boolean&nbsp;<wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; ">1&nbsp;<wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; ">true,false&nbsp;<wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; ">false</p><p align="left" style="line-height: normal; ">字节型&nbsp;<wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; ">byte&nbsp;<wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; ">8&nbsp;<wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; ">-128-127&nbsp;<wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; ">0</p><p align="left" style="line-height: normal; ">字符型&nbsp;<wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; ">char&nbsp;<wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; ">16&nbsp;<wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; ">&#8216;\u000&#8217;-\uffff&nbsp;<wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; ">&#8216;\u0000&#8217;</p><p align="left" style="line-height: normal; ">短整型&nbsp;<wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; ">short&nbsp;<wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; ">16&nbsp;<wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; ">-32768-32767&nbsp;<wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; ">0</p><p align="left" style="line-height: normal; ">整型&nbsp;<wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; ">int&nbsp;<wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; ">32&nbsp;<wbr style="line-height: normal; ">-2147483648,2147483647&nbsp;<wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; ">0</p><p align="left" style="line-height: normal; ">长整型&nbsp;<wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; ">long&nbsp;<wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; ">64&nbsp;<wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; ">-9.22E18,9.22E18&nbsp;<wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; ">0</p><p align="left" style="line-height: normal; ">浮点型&nbsp;<wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; ">float&nbsp;<wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; ">32&nbsp;<wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; ">1.4E-45-3.4028E+38&nbsp;<wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; ">0.0</p><p align="left" style="line-height: normal; ">双精度型&nbsp;<wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; ">double&nbsp;<wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; ">64&nbsp;<wbr style="line-height: normal; ">4.9E-324,1.7977E+308&nbsp;<wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; ">0.0</p><p align="left" style="line-height: normal; ">这里特别要提出出的两种类型：</p><p align="left" style="line-height: normal; ">BigInteger 任意大的整数，原则上是，只要你的计算机的内存足够大，可以有无限位的</p><p align="left" style="line-height: normal; ">BigInteger 任意大的实数，可以处理小数精度问题。</p><p align="left" style="line-height: normal; ">BigInteger中一些常见的函数：</p><p align="left" style="line-height: normal; ">A=BigInteger.ONE</p><p align="left" style="line-height: normal; ">B=BigInteger.TEN</p><p align="left" style="line-height: normal; ">C=BigInteger.ZERO</p><p align="left" style="line-height: normal; ">一些常见的数的赋初值。将int型的数赋值给BigInteger，BigInteger.valueOf(k);</p><p align="left" style="line-height: normal; ">基本的函数：</p><p align="left" style="line-height: normal; ">valueOf:赋初值</p><p align="left" style="line-height: normal; ">add:+&nbsp;<wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; ">a.add(b);</p><p align="left" style="line-height: normal; ">subtract:-</p><p align="left" style="line-height: normal; ">multiply:*</p><p align="left" style="line-height: normal; ">divide:/</p><p align="left" style="line-height: normal; ">remainder：this % val</p><p align="left" style="line-height: normal; ">divideAndRemainder：a[0]=this / val; a[1]=this % val</p><p align="left" style="line-height: normal; ">pow：a.pow(b)=a^b</p><p align="left" style="line-height: normal; ">gcd,abs:公约数，绝对值</p><p align="left" style="line-height: normal; ">negate：取负数</p><p align="left" style="line-height: normal; ">signum：符号函数</p><p align="left" style="line-height: normal; ">mod：a.mod(b)=a%b;</p><p align="left" style="line-height: normal; ">shiftLeft:左移，this &lt;&lt; n ，this*2^n;</p><p align="left" style="line-height: normal; ">shiftRight:右移，this &gt;&gt; n，this/2^n;</p><p align="left" style="line-height: normal; ">and:等同于c++的&amp;&amp;,且；</p><p align="left" style="line-height: normal; ">or：||，或;</p><p align="left" style="line-height: normal; ">xor:异或，BigInteger xor(BigInteger val),this^val</p><p align="left" style="line-height: normal; ">not:!,非；</p><p align="left" style="line-height: normal; ">bitLength：返回该数的最小二进制补码表示的位的个数，即 *不包括* 符号位 (ceil(log2(this &lt;0 ? -this : this + 1)))。对正数来说，这等价于普通二进制表示的位的个数。</p><p align="left" style="line-height: normal; ">bitCount：返回该数的二进制补码表示中不包扩符号位在内的位的个数。该方法在 BigIntegers 之上实现位向量风格的集合时很有用。</p><p align="left" style="line-height: normal; ">isProbablePrime：如果该 BigInteger 可能是素数，则返回 true ；如果它很明确是一个合数，则返回 false 。 参数 certainty 是对调用者愿意忍受的不确定性的度量：如果该数是素数的概率超过了 1 - 1/2**certainty方法，则该方法返回 true 。执行时间正比于参数确定性的值。</p><p align="left" style="line-height: normal; ">compareTo：根据该数值是小于、等于、或大于 val 返回 -1、0 或 1；</p><p align="left" style="line-height: normal; ">equals：判断两数是否相等，也可以用compareTo来代替；</p><p align="left" style="line-height: normal; ">min，max：取两个数的较小、大者；</p><p align="left" style="line-height: normal; ">intValue，longValue，floatValue，doublue：把该数转换为该类型的数的值。</p><p align="left" style="line-height: normal; "><wbr style="line-height: normal; "></p><p align="left" style="line-height: normal; "><wbr style="line-height: normal; "></p><p align="left" style="line-height: normal; "><wbr style="line-height: normal; "></p><p align="left" style="line-height: normal; "><wbr style="line-height: normal; "></p><p align="left" style="line-height: normal; ">今天参考课本写了一个关于二进制与十进制转换的程序，程序算法不难，但写完后测试发现不论是二转十还是十转二，对于大于21亿即超过整数范围的数不能很好的转换。都会变成0.<br style="line-height: normal; " />参考书籍发现使用使用BigInteger可以解决这个问题。<br style="line-height: normal; " />于是查找了下JDK,然后测试几次终于写成功了！<br style="line-height: normal; " />使用心得如下：</p><p align="left" style="line-height: normal; ">1，BigInteger属于java.math.BigInteger,因此在每次使用前都要import 这个类。偶开始就忘记import了，于是总提示找不到提示符。</p><p align="left" style="line-height: normal; ">2，其构造方法有很多，但现在偶用到的有： BigInteger(String val)<br style="line-height: normal; " /><wbr style="line-height: normal; ">&nbsp;<wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; ">将 BigInteger 的十进制字符串表示形式转换为 BigInteger。<br style="line-height: normal; " />BigInteger(String val, int radix)<br style="line-height: normal; " /><wbr style="line-height: normal; ">&nbsp;<wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; ">将指定基数的 BigInteger 的字符串表示形式转换为 BigInteger。<br style="line-height: normal; " />如要将int型的2转换为BigInteger型，要写为BigInteger two=new BigInteger("2"); //注意2双引号不能省略</p><p align="left" style="line-height: normal; ">3，BigInteger类模拟了所有的int型数学操作，如add()==&#8220;+&#8221;,divide()==&#8220;-&#8221;等，但注意其内容进行数学运算时不能直接使用数学运算符进行运算，必须使用其内部方法。而且其操作数也必须为BigInteger型。<br style="line-height: normal; " />如：two.add(2)就是一种错误的操作，因为2没有变为BigInteger型。</p><p align="left" style="line-height: normal; ">4，当要把计算结果输出时应该使用.toString方法将其转换为10进制的字符串，详细说明如下：<br style="line-height: normal; " /><wbr style="line-height: normal; ">String toString()<br style="line-height: normal; " /><wbr style="line-height: normal; ">&nbsp;<wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; ">返回此 BigInteger 的十进制字符串表示形式。<br style="line-height: normal; " />输出方法：System.out.print(two.toString());</p><p align="left" style="line-height: normal; ">5,另外说明三个个用到的函数。&nbsp;<wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; ">BigInteger remainder(BigInteger val)<br style="line-height: normal; " /><wbr style="line-height: normal; ">&nbsp;<wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; ">返回其值为 (this % val) 的 BigInteger。<br style="line-height: normal; " /><wbr style="line-height: normal; ">BigInteger negate()<br style="line-height: normal; " /><wbr style="line-height: normal; ">&nbsp;<wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; ">返回其值是 (-this) 的 BigInteger。<br style="line-height: normal; " /><wbr style="line-height: normal; ">int&nbsp;<wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; ">compareTo(BigInteger val)<br style="line-height: normal; " /><wbr style="line-height: normal; ">&nbsp;<wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; "><wbr style="line-height: normal; ">将此 BigInteger 与指定的 BigInteger 进行比较。<br style="line-height: normal; " />remainder用来求余数。<br style="line-height: normal; " />negate将操作数变为相反数。<br style="line-height: normal; " />compare的详解如下：</p><p align="left" style="line-height: normal; ">compareTo<br style="line-height: normal; " />public int compareTo(BigInteger val)将此 BigInteger 与指定的 BigInteger 进行比较。对于针对六个布尔比较运算符 (&lt;, ==, &gt;, &gt;=, !=, &lt;=) 中的每一个运算符的各个方法，优先提供此方法。执行这些比较的建议语句是：(x.compareTo(y) &lt;op&gt; 0)，其中 &lt;op&gt; 是六个比较运算符之一。<br style="line-height: normal; " />指定者：<br style="line-height: normal; " />接口 Comparable&lt;BigInteger&gt; 中的 compareTo<br style="line-height: normal; " />参数：<br style="line-height: normal; " />val - 将此 BigInteger 与之比较的 BigInteger。<br style="line-height: normal; " />返回：</p><p align="left" style="line-height: normal; ">将BigInteger的数转为2进制：</p><p align="left" style="line-height: normal; ">public class TestChange {<br style="line-height: normal; " />public static void main(String[] args) {<br style="line-height: normal; " />System.out.println(change("3",10,2));<br style="line-height: normal; " />}<br style="line-height: normal; " />//num 要转换的数 from源数的进制 to要转换成的进制<br style="line-height: normal; " />private static String change(String num,int from, int to){<br style="line-height: normal; " />return new java.math.BigInteger(num, from).toString(to);<br style="line-height: normal; " />}<br style="line-height: normal; " />}</p></span></div> <div id="chromeVisPage2ExtensionDiv" style="display: none; ">Ctrl+v</div><div id="chromeVisExtension2PageDiv" style="display: none; "></div><span style="border-top-color: #000000; border-right-color: #000000; border-bottom-color: #000000; border-left-color: #000000; border-top-width: medium; border-right-width: medium; border-bottom-width: medium; border-left-width: medium; border-top-style: groove; border-right-style: groove; border-bottom-style: groove; border-left-style: groove; position: absolute; z-index: 100000000000; min-height: 5px; border-top-left-radius: 7px 7px; border-top-right-radius: 7px 7px; border-bottom-right-radius: 7px 7px; border-bottom-left-radius: 7px 7px; display: none; top: 0px; min-width: 993px; max-width: 993px; left: 10px; right: 100px; "></span><div id="chromeVisBackground2LensDiv" style="display: none; "></div><img src ="http://www.cppblog.com/aswmtjdsj/aggbug/153973.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/aswmtjdsj/" target="_blank">BUPT-[aswmtjdsj] @ Penalty</a> 2011-08-20 19:36 <a href="http://www.cppblog.com/aswmtjdsj/archive/2011/08/20/153973.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>