ACM competition Java programming summary, acm competition java programming
1. String Length
String str = new String ("abcd ");
Int length = str. length ();
2. Length of the array
2.1 For a [] [] a. length represents the number of rows of a [I]. length represents the number of columns of a. 2.2 for a [] a. length represents the length of.
3. Conversion of strings and character Arrays
String str = new String ("abcd ");
Char [] a = str. toCharArray ();
4. Conversion of string numbers and numbers
4.1 String-> int
String str = "1234 ";
Int I = Integer. parseInt (str); // an Exception may be thrown. If an error is reported, throws Exception is added after main (String [] args ).
4.2 int-> String
Int I = 235;
String s = String. valueOf (I); // Of uppercase
5. import data from the. in File
Import java. IO .*;
Public static void main (String [] args) throws IOException {// an exception is thrown.
FileReader a = new FileReader ("D-small-attempt1.in"); // The file is in the same directory as the source code
BufferedReader read = new BufferedReader ();
String textLine = "";
String str = "";
While (textLine = read. readLine ())! = Null ){
Str + = textLine + "";
}
String [] numbersArray = str. split (""); // separate str by space and save it as a character array
}
6.import data to the. txt file
Import java. IO .*;
FileWriter fw = new FileWriter ("output.txt ");
BufferedWriter bufw = new BufferedWriter (fw );
String line = null;
For (int I = 1; I <n + 1; I ++ ){
Bufw. write ("Case #" + I + ":" + result [I]);
Bufw. newLine (); // The next row
Bufw. flush (); // update
}
Bufw. close ();
7. Reserved limit Decimals
Import java. text .*
DecimalFormat sim = new DecimalFormat ("0.000000"); // The number of digits that are retained after the decimal point.
Double six = sim. format (source); // keep the source six decimal places
Relatively simple method (there is a limit number for the output result)
System. out. printf ("%. 3f", );