HDU高精度總結(java大數類)

來源:互聯網
上載者:User

標籤:java   hdu   高精度   

  HDU1002   A + B Problem II

【題意】大數相加

【連結】http://acm.hdu.edu.cn/showproblem.php?pid=1002

Sample Input
21 2112233445566778899 998877665544332211
 Sample Output
Case 1:1 + 2 = 3Case 2:112233445566778899 + 998877665544332211 = 1111111111111111110

代碼:

import java.io.*;import java.util.*;import java.math.BigInteger;//聲明BigInteger大數類import java.lang.*;public class Main{    public static void main(String args[])    {        Scanner cin = new Scanner(System.in);        int t,i=1;        t = cin.nextInt();        int tot = 0;        BigInteger a,b,c; //BigInteger類型        while (i<=t)        {            a=cin.nextBigInteger();            b=cin.nextBigInteger();            c=a.add(b);            System.out.println("Case "+i+":");            System.out.println(a+" + "+b+" = "+c);            if(i<t) System.out.println("");            i++;        }    }}

HDU1042 N!

【題意】大數階乘

【連結】http://acm.hdu.edu.cn/showproblem.php?pid=1042

Sample Input
123
 Sample Output
126

代碼:

import java.io.*;import java.util.*;import java.math.BigInteger;//聲明BigInteger大數類import java.lang.*;public class FF{    public static void main(String args[])    {        Scanner cin = new Scanner(System.in);        while(cin.hasNext())        {         int n=cin.nextInt();         BigInteger ans=BigInteger.ONE;         for(int i=1; i<=n; ++i)         {        ans=ans.multiply(BigInteger.valueOf(i));         }         System.out.println(ans);         System.gc();//調用記憶體回收機制        }    }}
HDU 1047 Integer Inquiry

【題意】多個大數相加

【連結】http://acm.hdu.edu.cn/showproblem.php?pid=1047

Sample Input
11234567890123456789012345678901234567890123456789012345678901234567890123456789012345678900
 
Sample Output
370370367037037036703703703670
注意下格式

代碼:

import java.io.*;import java.util.*;import java.math.BigInteger;//聲明BigInteger大數類import java.lang.*;public class Main{    public static void main(String args[])    {        Scanner cin = new Scanner(System.in);        int n=cin.nextInt();        while(n-->0)        {         BigInteger a,b,c;         b=BigInteger.ZERO;         while(cin.hasNextBigInteger())         {         c=BigInteger.ZERO;         c=cin.nextBigInteger();         if(!c.equals(BigInteger.valueOf(0)))         b=b.add(c);         else          {         System.out.println(b);         if(n!=0)         System.out.println("");         break;         }         }         System.gc();        }    }}

HDU 1715  大菲波數

【題意】RT

【連結】http://acm.hdu.edu.cn/showproblem.php?pid=1715

Sample Input
512345
 Sample Output
11235

代碼:

import java.io.*;import java.util.*;import java.math.BigInteger;//聲明BigInteger大數類import java.lang.*;public class Main{    public static void main(String args[])    {        Scanner cin = new Scanner(System.in);        int n=cin.nextInt();         BigInteger fac[]= new BigInteger[1001];        fac[0]=BigInteger.ZERO;//初始賦值        fac[1]=BigInteger.ONE;        for(int i=2; i<=1000; ++i) fac[i]=fac[i-1].add(fac[i-2]);        while(n-->0)        {        int a;        a=cin.nextInt();        System.out.println(fac[a]);        }         //System.gc();    }}

 HDU 1063  Exponentiation

【題意】高精度冪

【連結】http://acm.hdu.edu.cn/showproblem.php?pid=1063

Sample Input
95.123 120.4321 205.1234 156.7592  998.999 101.0100 12
 Sample Output
548815620517731830194541.899025343415715973535967221869852721.0000000514855464107695612199451127676715483848176020072635120383542976301346240143992025569.92857370126648804114665499331870370751166629547672049395302429448126.76412102161816443020690903717327667290429072743629540498.1075960194566517745610440100011.126825030131969720661201

最簡形式是去掉後面的 0,以及小於 1 的小數的小數點前的 0

實現高精度冪java方法:

(1)調用pow函數

(2)for迴圈

代碼:

import java.io.*;import java.util.*;import java.math.BigDecimal;import java.math.BigInteger;//聲明BigInteger大數類import java.lang.*;public class Main{    public static void main(String args[])    {        Scanner cin = new Scanner(System.in);        while(cin.hasNext())        {        BigDecimal ans=cin.nextBigDecimal();        int n=cin.nextInt();        String res=ans.pow(n).stripTrailingZeros().toPlainString();//整數去掉小數點和後面的0
       if(res.startsWith("0"))//去掉前置0           {           res=res.substring(1);           }           System.out.println(res);       /*  BigDecimal a=BigDecimal.ONE;        int n=cin.nextInt();        for(int i=1; i<=n; ++i)        a=a.multiply(ans);        String res=a.stripTrailingZeros().toPlainString();           if(res.startsWith("0"))           {           res=res.substring(1);           }           System.out.println(res);       */        }    }}

HDU 1316   How Many Fibs?

【題意】區間fibonacci

【連結】http://acm.hdu.edu.cn/showproblem.php?pid=1316

Sample Input
10 1001234567890 98765432100 0
 Sample Output
54

代碼:

import java.io.*;import java.util.*;import java.math.BigDecimal;import java.math.BigInteger;//聲明BigInteger大數類public class Main{    public static void main(String args[])    {        Scanner cin = new Scanner(System.in);        BigInteger a,b;         int ans,i;        BigInteger fac[]=new BigInteger[1005];        BigInteger zero=BigInteger.ZERO;        fac[1]=BigInteger.valueOf(1);        fac[2]=BigInteger.valueOf(2);        for(i=3; i<1005; ++i) fac[i]=fac[i-1].add(fac[i-2]);        while(cin.hasNextBigInteger())        {           a=cin.nextBigInteger();           b=cin.nextBigInteger();           if(a.compareTo(zero)==0&&b.compareTo(zero)==0) break;           for(ans=0,i=1; i<1005; ++i)           {           if(a.compareTo(fac[i])<=0&&b.compareTo(fac[i])>=0) ans++;           if(b.compareTo(fac[i])<0) break;           }          System.out.println(ans);        }    }}


HDU 1753   大明A+B (高精度)

【題意】高精度小數相加

【連結】http://acm.hdu.edu.cn/showproblem.php?pid=1753

Sample Input
1.1 2.91.1111111111 2.34443233431 1.1
 Sample Output
43.45554344542.1

代碼:

import java.io.*;import java.util.*;import java.math.BigDecimal;import java.math.BigInteger;//聲明BigInteger大數類public class Main{    public static void main(String args[])    {        Scanner cin = new Scanner(System.in);        BigDecimal a,b,c;        while(cin.hasNextBigDecimal())        {        a=cin.nextBigDecimal();        b=cin.nextBigDecimal();        c=a.add(b);        String res=c.stripTrailingZeros().toPlainString();        System.out.println(res);        }    }}


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

HDU高精度總結(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.