【Java】-BigInteger大數類的使用【超強Java大數模板 總結】

來源:互聯網
上載者:User

標籤:

1. 單元變數常用大數操作:

import java.util.Scanner;import java.math.*;public class Main{public static void main(String args[]){Scanner cin= new Scanner(System.in);//使用Sacnner類建立cin對象BigInteger a, b;//建立大數對象while(cin.hasNext()){a=cin.nextBigInteger();b=cin.nextBigInteger();System.out.println("a+b="+a.add(b));System.out.println("a-b="+a.subtract(b));System.out.println("a*b="+a.multiply(b));System.out.println("a/b="+a.divide(b));System.out.println("a%b="+a.remainder(b));if(a.compareTo(b)==0) //比較兩數的大小System.out.println("a==b");else if(a.compareTo(b)>0)System.out.println("a>b");else System.out.println("a<b");System.out.println(a.abs());//取絕對值int e=10;System.out.println(a.pow(e));//求a^eSystem.out.println(a.toString()); //將大數a轉字串輸出int p=8;System.out.println(a.toString(p)); //將大數a轉換成p進位後 按字串輸出}}}

 2.java大數 實現遞推公式:f[i]=f[i-1]+f[i-2]*2 

 

 

import java.util.*;import java.math.*; //匯入類public class Main{    static BigInteger[] ans; //定義全域變數    public static void main(String[] args){        Scanner reader=new Scanner(System.in);        //定義Scanner類對象        ans = new BigInteger[251]; //定義ans大數數組的大小        ans[0]=BigInteger.valueOf(1);//大數賦初值        ans[1]=BigInteger.valueOf(1);        ans[2]=BigInteger.valueOf(3);        for(int i=3; i<=250; i++)        {            ans[i] = ans[i-1].add(ans[i-2].multiply(BigInteger.valueOf(2)));        }  //大數加法的使用        int n;        while(reader.hasNextInt()){            n=reader.nextInt();            System.out.println(ans[n]); //普通輸出        }    }}

 

【Java】-BigInteger大數類的使用【超強Java大數模板 總結】

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.