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:
The format is: interval cin = new second (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)
{
Partition cin = new partition (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)
{
Partition cin = new partition (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 digit, # 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;
Partition cin = new partition (new BufferedInputStream (System. in ));
String st = "abcdefg ";
System. out. println (st. charAt (0); // st. charAt (I) is equivalent to st [I].
Char [] ch;
Ch = st. toCharArray (); // converts a string to a character array.
For (I = 0; I <ch. length; I ++) ch [I] + = 1;
System. out. println (ch); // The input is "bcdefgh ".
If (st. startsWith ("a") // if the string starts with '0.
{
St = st. substring (1); // start from 1st bits and copy (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, etc. The addition, subtraction, multiplication, and Division modes must be the operations between BigInteger (BigDecimal) and BigInteger (BigDecimal) type conversion to BigInteger (BigDecimal), using 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)
{
Partition cin = new partition (new BufferedInputStream (System. in ));
Int a = 123, B = 456, c = 7890;
BigInteger x, y, z, ans;
X = BigInteger. valueOf (a); 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 into 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, and the first is the string to be converted, the second is to explain what the hexadecimal format is ).
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: www.2cto.com
Import java. io .*;
Import java. math .*;
Import java. util .*;
Import java. text .*;
Public class Main
{
Public static void main (String [] args)
{
Partition cin = new partition (new BufferedInputStream (System. in ));
Int n = cin. nextInt ();
Int a [] = new int [n];
For (int I = 0; I <n; I ++) a [I] = cin. nextInt ();
Arrays. sort ();
For (int I = 0; I <n; I ++) System. out. print (a [I] + "");
}
}