码迷,mamicode.com
首页 > 编程语言 > 详细

java的BigInteger使用指南

时间:2015-02-08 19:28:12      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:

先上一个昨天写的破快速幂:

(注:hdu交题的时候public class那里的类名是Main)

 1 import java.util.*;
 2 import java.math.*;
 3 
 4 public class Main{
 5     public static void main(String[] args)
 6     {
 7         BigInteger a,c,aa,cc;
 8         int b,d,ans;
 9         Scanner in=new Scanner(System.in);
10         while (in.hasNext())
11         {
12             aa=new BigInteger("1");
13             cc=new BigInteger("1");
14             a=in.nextBigInteger();
15             b=in.nextInt();
16             c=in.nextBigInteger();
17             d=in.nextInt();
18             if ((a.compareTo(c)==1)&&(b>d))
19                 ans=1;
20             else if ((a.compareTo(c)==-1)&&(b<d))
21                 ans=-1;
22             else
23             {
24                 while (b>0)
25                 {
26                     if (b%2!=0)
27                         aa=aa.multiply(a);
28                     b=b/2;
29                     a=a.multiply(a);
30                 }
31                 while (d>0)
32                 {
33                     if (d%2!=0)
34                         cc=cc.multiply(c);
35                     d=d/2;
36                     c=c.multiply(c);
37                 }
38                 
39                 //System.out.println(aa+"  "+cc);
40                 ans=aa.compareTo(cc);
41             }
42             if (ans==0)
43                 System.out.println("=");
44             else if (ans==-1)
45                 System.out.println("<");    //aa<cc
46             else if (ans==1)
47                 System.out.println(">");    //aa>cc
48         }
49     }
50 }

 

BigInteger类常用的方法:

add:+  subtract:-  multiply:*  divide:/  remainder:%(mod)

a.pow(b):a^b  abs:绝对值

compareTo:比较大小

min、max:取最值

valueOf:赋初值(或者用上面的newBigInteger)

 

 

Reference:

http://www.cppblog.com/aswmtjdsj/archive/2011/08/20/153973.aspx

java的BigInteger使用指南

标签:

原文地址:http://www.cnblogs.com/pdev/p/4280290.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!