java 大數計算


這幾天做了幾道用大數的題,發現java來做大數運算十分方便。對acmer來說是十分實用的

 

 

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.punlic int comareTo();

14.boolean equals(); 是否相等

15.BigInteger構造函數:

 1 import java.io.*;
 2 import java.math.BigInteger;
 3 import java.util.*;
 4 public class Main {
 5 
 6     public static void main(String[] args) {
 7         // TODO Auto-generated method stub
 8         Scanner cin=new Scanner (new BufferedInputStream(System.in));
 9         PrintWriter cout=new PrintWriter(System.out);
10         int t;
11         t=cin.nextInt();
12         int eg=1;
13         while(t>0)
14         {
15             BigInteger eight=new BigInteger("8");
16             BigInteger seven=new BigInteger("7");
17             BigInteger one=new BigInteger("1");
18             String inp;
19             inp=cin.next();
20             BigInteger n=new BigInteger(inp);
21             BigInteger ans;
22             BigInteger ans2;
23             ans=n.multiply(n).multiply(eight);
24             ans2=n.multiply(seven);
25             ans=ans.subtract(ans2);
26             ans=ans.add(one);
27             cout.println("Case #"+eg+": "+ans);
28             ++eg;
29             --t;
30             
31         }
32         cout.flush();
33     }
34 
35 }

大數的所有計算都是與大數之間進行的

BIgInteger a,b;

a=a.add(b);

a=a.substract(b);

a=a.multiply(b);

a=a.divied(b);

a=a.gcd(b);


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM