Java large number practice second play

Source: Internet
Author: User
Tags array to string

HDU1250 Water Topic Link: http://acm.hdu.edu.cn/showproblem.php?pid=1250

Import Java.util.*;import Java.math.biginteger;public class main{public    static void Main (string[] args) {        int a ;        Scanner in=new Scanner (system.in);        while (In.hasnext ()) {            a=in.nextint ();            Biginteger[] Num=new biginteger[10000];            Num[1]=biginteger.one;            Num[2]=biginteger.one;            Num[3]=biginteger.one;            Num[4]=biginteger.one;            for (int i=5;i<=a;i++) {                                num[i]= (num[i-1]). Add (Num[i-2]). Add (Num[i-3]).            System.out.println (Num[a]);}}    

hdu1297 Recursive solution Topic link http://acm.hdu.edu.cn/showproblem.php?pid=1297

Import Java.util.*;import Java.math.biginteger;public class main{public    static void Main (string[] args) {        Scanner in= New Scanner (system.in);        int A;        Biginteger[] b= new biginteger[1010];        B[1]=biginteger.one;        B[2]=biginteger.valueof (2);        B[3]=biginteger.valueof (4);        B[4]=biginteger.valueof (7);        while (In.hasnext ()) {            a=in.nextint ();            if (a<5)                System.out.println (B[a]);            else{for                (int i=5;i<=a;i++) {                    b[i]=b[i-1].add (B[i-2]). Add (B[i-4]);                }                System.out.println (B[a]);}}}            

hdu1715 Water Topic link http://acm.hdu.edu.cn/showproblem.php?pid=1715

Import Java.util.*;import Java.math.biginteger;public class main{public    static void Main (string[] args) {        Scanner in= New Scanner (system.in);        Biginteger[] b= new biginteger[1010];        B[1]=biginteger.valueof (1);        B[2]=biginteger.valueof (1);        for (int i=3;i<=1000;i++) {            b[i]=b[i-1].add (b[i-2]);        }        int N=in.nextint ();        while (n-->0) {            int text=in.nextint ();            System.out.println (B[text]);}}    

hdu1753 High-precision Decimal summation topic link http://acm.hdu.edu.cn/showproblem.php?pid=1753

Import Java.util.*;import Java.math.bigdecimal;public class main{public    static void Main (string[] args) {        Scanner in= New Scanner (system.in);        BigDecimal A, B;        while (In.hasnext ()) {            a=in.nextbigdecimal ();            B=in.nextbigdecimal ();            BigDecimal C=a.add (b);             String res = C.striptrailingzeros (). toplainstring (); Striptrailingzeros () Removes the excess 0                                                                  //toplainstring (), cancels the scientific notation notation              System.out.println (res);}        }    

Hdu 1865 find a regular topic link: http://acm.hdu.edu.cn/showproblem.php?pid=1865

Import Java.util.*;import java.math.*;p ublic class main{public    static void Main (string[] args) {        Scanner in= new Scanner (system.in);        int A;        String test;        A=in.nextint ();        Biginteger[] num= new biginteger[210];        Num[1]=biginteger.one;        Num[2]=biginteger.valueof (2);        Num[3]=biginteger.valueof (3);        while (a-->0) {            test=in.next ();            int n=test.length ();            if (n<4)                System.out.println (Num[n]);            else            {for                (int i=4;i<=n;i++) {                    num[i]=num[i-1].add (num[i-2]);                }                System.out.println (Num[n]);}}}    

HDU 2100 binary conversion, the method may be cumbersome but also proficient in many Java usages.

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=2100

Import Java.util.*;import java.math.*;p ublic class main{public static BigInteger mpow (int b) {//self-written exponentiation function bigin        Teger Ans=biginteger.one;        for (int i=0;i<b;i++) {ans=ans.multiply (biginteger.valueof (26));    } return ans;        public static String change (BigInteger num) {char[] line=new char[250];        int cnt=0;            while (Num.compareto (Biginteger.zero)!=0) {BigInteger a=num.mod (biginteger.valueof (26));            Num=num.divide (Biginteger.valueof (26)); Line[cnt++]= (char) (A.intvalue () + ' a '); Convert BigInteger to int} return string.valueof (line);        Converts a character array to String} public static void Main (string[] args) {Scanner in= new Scanner (system.in);            while (In.hasnext ()) {String A, B;            A=in.next ();            B=in.next ();            BigInteger An=biginteger.zero;            System.out.println (a);            SYSTEM.OUT.PRINTLN ((int) A.charat (1)); for (int i=0;i<a.length (); i++) {An=an.add (biginteger.valueof (A.charat (i)-' a '). Multiply (Mpow (a.length ()-i-1));//string            Single character display with Str.charat (i);            } BigInteger Bn=biginteger.zero; for (int i=0;i<b.length (); i++) {Bn=bn.add (biginteger.valueof (B.charat (i)-' A '). Multiply (Mpow (b.length)-i-            1)));            } BigInteger C=an.add (BN);            String Fin=change (c); for (int i=fin.length () -1;i>=0;i--) {if (Fin.charat (i)! = ') ') System.out.print (Fin.charat (i            ));                    } System.out.println (); }    }}


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

Java large number practice second play

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.