Java Large number

Source: Internet
Author: User

Transferred from: https://www.cnblogs.com/zufezzt/p/4794271.html

ImportJava.util.*;Importjava.math.*; Public classmain{ Public Static voidMain (String args[]) {Scanner cin=NewScanner (system.in);             BigInteger A, B; //End With EOF file        while(Cin.hasnext ()) {a=Cin.nextbiginteger (); b=Cin.nextbiginteger (); System.out.println (A.add (b)); //Large integer AdditionSystem.out.println (A.subtract (b));//Large integer SubtractionSystem.out.println (a.multiply (b));//Large integer MultiplicationSystem.out.println (A.divide (b));//Large integer division (rounding)System.out.println (A.remainder (b));//Large integer modulus//comparison of large integers           if(A.compareto (b) = = 0) System.out.println ("a = = B");//Large integer a==b           Else if(A.compareto (b) > 0) System.out.println ("a > B");//Large integer a>b           Else if(A.compareto (b) < 0) System.out.println ("a < B");//Large integer a<b//Large integer Absolute valueSystem.out.println (A.abs ());//the absolute value of a large integer//the power of large integers           intexponent=10; System.out.println (A.pow (exponent)); //exponent power of large integer a//returns a string representation of a large integer decimalSystem.out.println (a.tostring ()); //returns a string representation of a large integer p-binary           intP=8;       System.out.println (A.tostring (p)); }    }}

Turn from: 51884888

HDU 1002a+b Large number version
1 ImportJava.math.BigInteger;2 ImportJava.util.Scanner;3 4  Public classMain {5     voidsolve () {6 BigInteger A, B, C;7Scanner cin =NewScanner (system.in);8         intt =cin.nextint ();9          for(inti = 1; I <= t; i++) {TenSYSTEM.OUT.PRINTLN ("case" + i + ":"); OneA =Cin.nextbiginteger (); Ab =Cin.nextbiginteger (); -System.out.println (A + "+" + B + "=" +A.add (b)); -             if(I! =t) System.out.println (); the         } -     } -      Public Static voidMain (string[] args) { -Main work =NewMain (); + work.solve (); -     } +}

HDU 1042 factorial Large number version
1 ImportJava.math.BigInteger;2 ImportJava.util.Scanner;3 4  Public classMain {5     intMAXN = 10005;6     voidsolve () {7Scanner cin =NewScanner (system.in);8         intN;9          while(Cin.hasnext ()) {Tenn =cin.nextint (); OneBigInteger ans = biginteger.valueof (1); A              for(inti = 2; I <= N; i++) { -Ans =ans.multiply (biginteger.valueof (i)); -             } the System.out.println (ans); -         } -     } -      Public Static voidMain (string[] args) { +Main work =NewMain (); - work.solve (); +     } A}

HDU 1297F(N)=f (n? 1) + F (n 2) +f ( N? 4) f (N) =f (n?1) +f (n?2) +f (n?4)
Importjava.math.*;ImportJava.util.*; Public classMain {voidsolve () {Scanner cin=NewScanner (system.in); biginteger[] ans=Newbiginteger[1001]; ans[1] = biginteger.valueof (1); ans[2] = biginteger.valueof (2); ans[3] = biginteger.valueof (4); ans[4] = biginteger.valueof (7);  for(inti = 5; I <= 1000; i++) {Ans[i]= Ans[i-1].add (Ans[i-2].add (ans[i-4])); }         while(Cin.hasnext ()) {intn =Cin.nextint ();        System.out.println (Ans[n]); }    }     Public Static voidMain (string[] args) {main work=NewMain ();    Work.solve (); }}
HDU 1753 high-precision fractional a+b, to remove the end of the rear guide 0.
Importjava.math.*;ImportJava.util.*; Public classMain {voidsolve () {//BigInteger A, B, C;Scanner cin =NewScanner (system.in); BigDecimal a= bigdecimal.valueof (0); BigDecimal b= bigdecimal.valueof (0);  while(Cin.hasnext ()) {a=Cin.nextbigdecimal (); b=Cin.nextbigdecimal ();        System.out.println (A.add (b). Striptrailingzeros (). toplainstring ()); }    }     Public Static voidMain (string[] args) {main work=NewMain ();    Work.solve (); }}

Java Large number

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.