HDU High-precision Summary (Java large number Class)

Source: Internet
Author: User

HDU1002 A + B Problem II

"Test Instructions" the sum of large numbers

"Link" http://acm.hdu.edu.cn/showproblem.php?pid=1002

Sample Input
21 2112233445566778899 998877665544332211
Sample Output
Case 1:1 + 2 = 3Case 2:1.,122,334,455,667,79e,+17 + 998877665544332211 = 1111111111111111110

Code:

Import java.io.*;import java.util.*;import java.math.biginteger;//Declaration BigInteger large Number class import java.lang.*;p ublic 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 type        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!

"Test Instructions" large number factorial

"Link" http://acm.hdu.edu.cn/showproblem.php?pid=1042

Sample Input
123
Sample Output
126

Code:

Import java.io.*;import java.util.*;import java.math.biginteger;//Declaration BigInteger large Number class import java.lang.*;p ublic 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 ();//Call garbage Collection Mechanism}}}    
HDU 1047 Integer Inquiry

"Test Instructions" multiple large numbers added

"Link" http://acm.hdu.edu.cn/showproblem.php?pid=1047

Sample Input
11234567890123456789012345678901234567890123456789012345678901234567890123456789012345678900

Sample Output
370370367037037036703703703670
Note the format

Code:

Import java.io.*;import java.util.*;import java.math.biginteger;//Declaration BigInteger large Number class import java.lang.*;p ublic 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 Large Fibonacci number

"Test Instructions" RT

"link"http://acm.hdu.edu.cn/showproblem.php?pid=1715

Sample Input
512345
Sample Output
11235

Code:

Import java.io.*;import java.util.*;import java.math.biginteger;//Declaration BigInteger large Number class import java.lang.*;p ublic 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;//Initial assignment value        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

"Test Instructions" high precision power

"Link" 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.00000005148554641076956121994511276767154838481760200726351 20383542976301346240143992025569.92857370126648804114665499331870370751166629547672049395302429448126.7641210216181644302 0690903717327667290429072743629540498.1075960194566517745610440100011.126825030131969720661201

The simplest form is to remove the back of the 0, and less than 1 decimal places before the decimal point 0

Implement high-precision power Java methods:

(1) Call the POW function

(2) For loop

Code:

Import Java.io.*;import java.util.*;import Java.math.bigdecimal;import java.math.biginteger;// Declaration BigInteger large Number class import java.lang.*;p ublic 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 ();//integer minus the decimal point and the following 0
       if (Res.startswith ("0"))//remove leading 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?

"Test Instructions" interval Fibonacci

"Link" http://acm.hdu.edu.cn/showproblem.php?pid=1316

Sample Input
10 1001234567890 98765432100 0
Sample Output
54

Code:

Import Java.io.*;import java.util.*;import Java.math.bigdecimal;import java.math.biginteger;// Declaration BigInteger large Number class 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 daming a+b (high precision)

"Test Instructions" high-precision fractional addition

"Link" 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

Code:

Import Java.io.*;import java.util.*;import Java.math.bigdecimal;import java.math.biginteger;// Declaration BigInteger large Number class 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);}}}    


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU High-precision Summary (Java large number Class)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.