Java large number of applications

Source: Internet
Author: User

1. Large number addition

1 ImportJava.math.BigInteger;2 ImportJava.util.Scanner;3 4 5  Public classMain {6      Public Static voidMain (string[] args) {7Scanner cin =NewScanner (system.in);//large number of inputs, define an input8BigInteger A =NULL, B =NULL, C =NULL;//start to assign value to empty9A = biginteger.valueof (100);TenB = biginteger.valueof (99); One         intT; AT = Cin.nextint ();//read into t; - //While (Cin.hasnextbiginteger ())//determines whether the end of the file is read as while (~SCANF ()) -          for(intcas = 1; CAS <= T; cas++) the         { -A =Cin.nextbiginteger (); -b =Cin.nextbiginteger (); -              + //BigInteger zero = biginteger.valueof (0);//A large number of judgments is not equal to 0 - //if (a.equals (biginteger.valueof (0))) {System.out.println ("haha");} + //if (a.equals (zero)) {System.out.println ("hehe");} Ac =A.add (b); at             if(CAs > 1) System.out.println ();//large number of line-break output -SYSTEM.OUT.PRINTLN ("case" + CAs + ":");//the output of the large number is connected with the + sign -System.out.println (A + "+" + B + "=" +c); -         } -Cin.close ();//turn off the read-in -     } in  -}

2. Large real numbers

1 ImportJava.math.BigDecimal;2 ImportJava.util.Scanner;3 4 5  Public classMain {6      Public Static voidMain (string[] args) {7Scanner s =NewScanner (system.in);8      while(S.hasnext ()) {9BigDecimal B =s.nextbigdecimal ();TenBigDecimal ans = bigdecimal.valueof (1); One         intn =s.nextint (); A          while(n--> 0) -Ans =ans.multiply (b); -String string =Ans.striptrailingzeros (). toplainstring (). toString (); the         if(String.startswith ("0."))) -String = string.substring (1); - System.out.println (string); -     } + s.close (); -     } +}

Java large number of applications

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.