Java usage in ACM

Source: Internet
Author: User

The main application of Java in ACM is the big data class [personal opinion ]. Make a summary and use it as a template.

The default class name is main.

Input:

Declare an input object CIN; CIN = new expect (system. In );

Enter an int value: int A = cin. nextint ();

Enter a large number: bigdecimal A = cin. nextbigdecimal ();

End of EOF: While (CIN. hasnext ()){...}

Output:

Output STR [any type]: system. Out. println (STR); [with line breaks] system. Out. Print (STR); [without line breaks]

System. Out. println ("str"); [Output string STR]

Assignment:Biginteger A = new biginteger (string. valueof (12 ));

Special Functions for large numbers:

Equal judgment: C. compareto (bigdecimal. Zero) = 0 [c =]

If the value is greater than: C. compareto (bigdecimal. Zero)> 0 [C is greater than 0]

Judge less than: C. compareto (bigdecimal. Zero) <0 [C less than 0]

Modify the format: C. striptrailingzeros (). toplainstring (); [C. Remove 0 at the end and convert it to another string]

String functions:

Str. startwith ("0"). [start with 0] Str. endwith ("0") [end with 0]

Str. substring (int x, int y) [substring of STR from X to Y] Str. substring (int x) [substring of STR from X to the end]

Biginteger functions:

Construction: Public biginteger (string Str) [convert string type data to a large integer]

Construction: Public biginteger POW (bigdecimal B) [power B]

Add: Public biginteger add (biginteger B) [add B]

Minus: Public biginteger subtract (biginteger B) [minus B]

Multiply: Public biginteger multiply (biginteger B) [multiply by B]

Except: Public biginteger divided (biginteger B) [Except B]

Maximum Value: Public biginteger max (biginteger A) [returns the maximum value]

Minimum value: Public biginteger min (biginteger A) [returns the minimum value]

Except: Public biginteger [] dividedandremainder (biginteger B) [in addition to B, the first digit of the array is the quotient, and the second digit is the remainder]

Bigdecimal functions:

Structure: Public bigdecimal (string Str) [convert string type data to high precision Data]

Public bigdecimal (INT Str) [convert int type data to a big integer]

Public bigdecimal (double Str) [convert data of the double type to a big integer]

Power: Public bigdecimal POW (bigdecimal B) [power B]

Add: Public bigdecimal add (bigdecimal B) [add B]

Minus: Public bigdecimal subtract (bigdecimal B) [minus B]

Multiply: Public bigdecimal multiply (bigdecimal B) [multiply by B]

Except: Public biginteger divided (biginteger B) [Except B]

Example:

A + B. Output the simplest result

Import Java. math. bigdecimal; // import a high-precision package into Java. util. *; // The package where the input and output are located. [almost all the Code must be included.] public class main {public static void main (string [] ARGs) {CIN = new partition (system. in); bigdecimal A, B, C; while (CIN. hasnext () {A = cin. nextbigdecimal (); B = cin. nextbigdecimal (); C =. add (B); If (C. compareto (bigdecimal. zero) = 0) {system. out. println ("0"); continue;} string STR = C. striptrailingzeros (). toplainstring (); If (Str. endswith (". ") STR = Str. substring (0, str. length ()-1); system. out. println (STR );}}}

A's factorial:

 import java.util.*;import java.math.*;public class Main{    public static void main(String args[])    {        int m,i;        Scanner in = new Scanner(System.in);        m=in.nextInt();        BigInteger sum = new BigInteger(String.valueOf(m));        for(i=m-1;i>0;i--)        {            BigInteger x =new BigInteger(String.valueOf(i));            sum=sum.multiply(x);        }        System.out.println(sum);    }}        

 

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.