ACM-Java Quick Start

Source: Internet
Author: User

The speed of Java mentioned here is limited to Java syntax, including input and output, Operation Processing, string and high-precision processing, conversion between systems, etc. It can solve some high-precision questions on OJ.

1. input:
Format: CIN = new partition
(New bufferedinputstream (system. In ));

Routine:
Import java. Io .*;
Import
Java. Math .*;
Import java. util .*;
Import java. Text .*;

Public class main
{
Public
Static void main (string [] ARGs)
{
Successfully CIN = new successfully
(New bufferedinputstream (system. In ));
Int A; Double B; biginteger C;
String st;
A = cin. nextint (); B = cin. nextdouble (); C =
Cin. nextbiginteger (); D = cin. nextline (); // each type has an input function.

}
}

2. Output
Function: system. Out. Print ();
System. Out. println (); system. Out. printf ();
System. Out. Print (); // cout
<...;
System. Out. println (); // cout <... <
Endl;
System. Out. printf (); // similar to printf in C.

Routine:
Import java. Io .*;
Import
Java. Math .*;
Import java. util .*;
Import java. Text .*;

Public class main
{
Public
Static void main (string [] ARGs)
{
Successfully CIN = new successfully
(New bufferedinputstream (system. In ));
Int A; Double B;
A =
12345; B = 1.234567;
System. Out. println (a + "" + B );

System. Out. printf ("% d % 10.5f \ n", a, B); // input B to the width of the word to 10, right alignment, retain the last five decimal places, rounding.

}
}

Normalized output:
Function:
//
Here 0 refers to a number, # refers to a Number other than 0 (if it is 0, it is not displayed), rounding.
Decimalformat FD = new
Decimalformat ("#. 00 #");
Decimalformat GD = new
Decimalformat ("0.000 ");
System. Out. println ("x =" + FD. Format (x ));

System. Out. println ("x =" + GD. Format (x ));

3.
String processing
The string in Java cannot be modified. to modify a string, it can only be converted to a character array.

Routine:
Import java. Io .*;
Import
Java. Math .*;
Import java. util .*;
Import java. Text .*;

Public class main
{
Public
Static void main (string [] ARGs)
{
Int I;
Bytes
Cin = new second (New bufferedinputstream (system. In ));
String ST =
"Abcdefg ";
System. Out. println (St. charat (0 ));//
St. charat (I) is equivalent to st.
Char [] ch;
Ch = ST. tochararray ();//
String to character array.
For (I = 0; I <Ch. length; I ++) CH + = 1;

System. Out. println (CH); // The input is "bcdefgh ".
If (St. startswith (""))//
If the string starts with '0.
{
St = ST. substring (1 );//
Copy starts from 1st bits (starts with 0th bits ).
}
}
}

4.
High Precision
Biginteger and bigdecimal are the primary reasons for acmer to select Java.
Function: add, subtract, divide,
MoD,
Compareto and so on. The addition, subtraction, multiplication, division, and Division modes require the calculation between biginteger (bigdecimal) and biginteger (bigdecimal). Therefore, you need to convert the INT (double) type to biginteger (bigdecimal) and use the biginteger function. valueof ().

Routine:
Import java. Io .*;
Import
Java. Math .*;
Import java. util .*;
Import java. Text .*;

Public class main
{
Public
Static void main (string [] ARGs)
{
Successfully CIN = new successfully
(New bufferedinputstream (system. In ));
Int A = 123, B = 456, c =
7890;
Biginteger X, Y, Z, ans;
X = biginteger. valueof ();
Y = biginteger. valueof (B); Z = biginteger. valueof (C );
Ans = x. Add (y );
System. Out. println (ANS );
Ans = Z. Divide (y );
System. Out. println (ANS );
Ans = x. Mod (z );
System. Out. println (ANS );
If (ANS. compareto (x) = 0)
System. Out. println ("1 ");
}
}

5.
Hexadecimal conversion
Java is a powerful function.
Function:
String ST = integer. tostring (Num, base );//
Convert num to the base (base <= 35 ).
Int num = integer. parseint (St, base );
// Use ST as the base and convert it to a 10-digit int (parseint has two parameters: the first is the string to be converted, and the second is the description of the base ).
Biginter
M = new biginteger (St, base); // st is a string, and base is the base.

6.
Sort
Function: arrays. Sort (); As for how to sort the struct, write a CMP method in C ++. It is not clear in Java. I hope someone can give me some advice ~~

Routine:
Import java. Io .*;
Import
Java. Math .*;
Import java. util .*;
Import java. Text .*;

Public class main
{
Public
Static void main (string [] ARGs)
{
Successfully CIN = new successfully
(New bufferedinputstream (system. In ));
Int n =
Cin. nextint ();
Int A [] = new int [N];
For (INT I = 0; I
<N; I ++) A = cin. nextint ();
Arrays. Sort ();
For (INT I
= 0; I <n; I ++) system. Out. Print (a + "");
}
}

7. poj high-precision question summary:
Poj 1131 1205 1220
1405 1503 1604 1894 2084 2305 2325 2389 2413 3101
3199

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.