use of java in ACM
This refers to the Java Express, limited to Java syntax, including input and output, processing, string and high-precision processing, conversion between the transformation, etc., can solve some of the high-precision problems on the OJ.
1. Input: Format: Scanner cin = new Scanner (new Bufferedinputstream (system.in)); Routines: Import Java.io.*;import Java.math.*;import Java.util.*;import java.text.*;p ublic class main{public static void Main (string[] args) {Scanner cin = new Scanner (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 a corresponding 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 the printf usages in C. Routines: Import java.io.*;import java.math.*;import java.util.*;import java.text.*;p ublic class main{Publ IC static void Main (string[] args) {Scanner cin = new Scanner (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 is a word width of 10, right-aligned, 5 digits after the decimal point, rounded. }} Normalized output: function://Here 0 refers to a digit, #指除0以外的数字 (if 0, not shown), rounded. 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 in Java string strings are not modifiable, and modifications can only be converted to character arrays. Routine: Import java.io.*;import java.math.*;import Java.util.*;import java.text.*;p ublic class main{public static void Main (string[] args) {int i; Scanner cin = new Scanner (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 (); The string is converted to a character array. for (i = 0; i < ch.length; i++) Ch[i] + = 1; SYSTEM.OUT.PRINTLN (CH); Enter as "Bcdefgh". if (St.startswith ("a"))//If the string starts with ' 0 '. {st = st.substring (1);//start Copy from 1th bit (beginning with No. 0 bit). }}} 4. High-precision BigInteger and BigDecimal can be said to be the primary reason for Acmer to choose Java. Function: AdD, subtract, divide, mod, CompareTo, etc., wherein the subtraction mode is required to be the operation between BigInteger (BigDecimal) and BigInteger (BigDecimal), so the int (double The type is converted to BigInteger (BigDecimal), with the function biginteger.valueof (). Routine: Import java.io.*;import java.math.*;import java.util.*; Import java.text.*;p ublic class main{public static void Main (string[] args) {Scanner cin = new Scanner (NE W 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. One of the most powerful features of the binary conversion java. function: String st = integer.tostring (num, base); Turn num as a 10-digit number into the base-<= St (base). int num = integer.parseint (st, base); Take St as base, turn to 10 int (parseint has two arguments, the first is the string to go, and the second is what the binary is). Biginter m = new BigInteger (St, base); St is a string, base is the binary of St.//added by ABILITYTAO1. If you want to read a large number in 2, you can use Cin.nextbiginteger (2); Of course, you can also use other input methods to read in; 2. If you want to convert a large number to a string other than the binary form, use Cin.tostring (2);//convert it to a 2-in-binary representation of the string routine: POJ 2305 import Java.io.*;import Java.util.*;import java.math.*;p ublic class main{public static void Main (string[] args) {int b; BigInteger P,m,ans; String str; Scanner cin = new Scanner (new Bufferedinputstream (system.in)); while (Cin.hasnext ()) {b=cin.nextint (); if (b==0) break; P=cin.nextbiginteger (b); M=cin.nextbiginteger (b); Ans=p.mod (m); Str=ans.tostring (b); System.out.println (str); }}}//end by Abilitytao 6. Sort function: Arrays.sort (); As for how to sort the structure, like C + + Write a CMP method, in Java is not very clear, I hope someone pointing to the next ~ ~: Import Java.io.*;import Java.math.*;import Java.util.*;import java.text.*;p ublic class main{public static void Main (string[] args) {Scanner cin = new Scanner (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 (a); for (int i = 0; i < n; i++) System.out.print (A[i] + ""); }}
7. POJ High-precision Topic Summary:
POJ 1131 1205 1220 1405 1503 1604 1894 2084 2305 2325 2389 2413 3101 3199
Transferred from : http://hi.baidu.com/czyuan_acm/blog/item/d0bf7a439d90d21b72f05d69.html
Application of Java in ACM
Chapter I.
Java Advantages and disadvantages of all kinds of books have , here only to talk about using Java to do ACM-ICPC features :
(1) The most obvious benefit is that learn Java, can participate in Java Challenge:)
(2) for programmers who are familiar with C + + , Java is not difficult to learn, to find a book, two weeks in the spare time can be done. Of course, it just means general programming, and it takes some time to get familiar with all of the Java libraries.
in fact, Java is only equivalent to an improved version of C + +, and all grammars are almost C + + , with few changes.
(3) in the general game, Java program will have extra time and space, and in fact, after the experiment, in the calculation of dense tasks in Java is not much slower than C + + , but the IO operation is slow.
(4) Java is simple and powerful, and some things are more convenient to implement in Java, such as high precision.
(5) It is not easy to make subtle mistakes in Java, such as pointers in C + + , "if (n = m) ... " and so
(6) Currently, Eclipse is a basic configuration and writing Java programs is easier to debug than C + + . There is one more option in a specific contest.
(7) Learning Java is good for future work. There are many places in the world where Java people are more likely to be than C + + .
(8) Java can make you look more like the hoof animal (ox) hoho~
Chapter II. Here are some of the problems that ACM-ICPC players encountered with Java programming in the first few days: 1. Basic input and output: (1) The New scanner class in JDK 1.5.0 provides a good basis for input and is simply for ACM-ICPC. General usage: Import java.io.* import java.util.* public class Main {public static void main (String args[]) { Scanner cin = new Scanner (new Bufferedinputstream (system.in)); ... } It is also possible to directly Scanner cin = new Scanner (system.in); Just adding buffer may be faster (2) read an integer: int n = cin.nextint (); Equivalent to scanf ("%d", &n); or CIN >> N; Read a string: string s = Cin.next (); Equivalent to scanf ("%s", s); or CIN >> s; Read a floating-point number: Double t = cin.nextdouble (); Equivalent to scanf ("%lf", &t); or cin >> T; Read a whole line: String s = cin.nextline (); Equivalent to gets (s); or Cin.getline (...); Determine if there is a next input can be used Cin.hasnext () or cin.hasnextint () or cin.hasnextdouble (), etc., see TOJ 1001 routines. (3) The output can be directly used System.out.print () and System.out.println (), the former does not output a newline, and the latter output. For example: System.out.println (n); The output of multiple integers on the same line as the int type can be System.out.println (new Integer (n). toStRing () + "" + New Integer (m). ToString ()); can also be redefined: static printwriter cout = new PrintWriter (new Bufferedoutputstream (System.out)); COUT.PRINTLN (n); (4) for the output floating point number to retain a few decimal points, you can use the DecimalFormat class, import java.text.*; DecimalFormat f = new DecimalFormat ("#.00#"); DecimalFormat g = new DecimalFormat ("0.000"); Double A = 123.45678, B = 0.12; System.out.println (F.format (a)); System.out.println (F.format (b)); System.out.println (G.format (b)); Here 0 refers to a digit, #指除0以外的数字. 2. Large numbers BigInteger and BigDecimal are classes already in the Java.math package that represent integers, which represent floating-point usage: You cannot use large numbers directly with symbols such as +,-for example: (import java.math.*)//need to introduce Java.math package BigInteger a = biginteger.valueof (100); BigInteger B = biginteger.valueof (50); BigInteger C = A.add (b)//C = a + B; The main methods can be used: BigInteger Add (BigInteger Other) BigInteger subtract (BigInteger other) BigInteger multiply (BigInteger Other) BigInteger divide (BigInteger other) BigInteger mods (BigInteger other) int compareTo (BigInteger other) static BigInt Eger valueOf (long x) outputs large numbers directly using SYSTEM.OUT.PRINTLN (a)。 3. String strings are used to store strings, and you can use the Charat method to remove one of the bytes, counting starting from 0: string a = "Hello"; A.charat (1) = ' E ' can get substrings with substring method, as in Example System.out.println (a.substring (0, 4))//output "Hell" note the characters on the 2nd parameter position are not included. Doing so makes S.substring (a, B) always have b-a characters. String connections can be directly with the + sign, such as String a = "Hello"; String B = "World"; System.out.println (A + "," + B + "!"); Output "Hello, world!" If you want to change a byte in a string directly, you can use a different StringBuffer class. 4. Call recursion (or other dynamic method) in the main class the main method must be public static void, there is a warning message when calling a non-static class in Main, you can first establish the object, and then call the method through the object: public class Main { ... void dfs (int a) {if (...) return; ... dfs (a+1); } public static void Main (String args[]) {... Main E = new main (); E.dfs (0); ... } } 5. Other things to note (1) Java is an object-oriented language, the thinking method needs to be transformed, the function of the inside is collectively referred to as the method, do not mistake. (2) There are some changes in the array in Java, the interior of a multidimensional array is actually pointers, so Java does not support fill multidimensional arrays. The array must be initialized after it is defined, such as int[] a = new int[100]; (3) Boolean type is Boolean, only true and false two values, in the IF (...)/while (...) conditions of the statement must beis a Boolean type. if (n% 2) in C + + ... cannot be compiled in Java. (4) below in the Java.util package several methods of Arrays class can replace the Memset, Qsort/sort and Bsearch:Arrays.fill () Arrays.sort () Arrays.binarysearch ()
Transferred from: http://hi.baidu.com/oak_wesley/blog/item/35839200fd9dc10e1d9583de.html
Use of Java in ACM (GO)