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