|
||||||||||
About Java
Since 29 September 2006 the Java programming language is supported at HDU Online Judge. J2SE Development Kit (JDK) 5.0 Update 8 is used to compile and run solutions written in Java. You may download JDK and read online documentation at java.sun.com. A
Solutions written in Java must contain a single public class named Main which is case-sensitive. This class must contain the method "public static void main(String[] args)". Furthermore, the solution may contain any number of nested classes and global non-public classes. Here is an example of A + B problem solution:
import java.io.*; import java.util.*; public class Main { public static void main(String args[]) { Scanner cin = new Scanner(System.in); int a, b; while(cin.hasNextInt()) { a = cin.nextInt(); b = cin.nextInt(); System.out.println(a + b); } } }The following solution is also correct: import java.util.*; class YouCanUseSuchClasses {} public class Main { class AndSuchClassesToo {} public static void main(String args[]) { Scanner cin = new Scanner(System.in); int a, b; while(cin.hasNextInt()) { a = cin.nextInt(); b = cin.nextInt(); System.out.println(a + b); } } } A Due to some peculiarities of the Java programming language, Java solutions often work slower and use more memory than solutions written in other programming languages regardless of problem difficulty. Nevertheless, it is guaranteed that almost all HDU Online Judge problems may be solved in Java as well as in other programming languages. For Java solutions, the time limit of each problem is always longer than other languages, and the memory limit is also larger. You can find these limits under each problem's title.
A Input/output operations in Java may be very slow in the case of wrong usage. Here are some rules which should be followed to avoid input/output performance problems:
import java.io.*; public class Main { public static void main(String[] args) throws IOException { StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); int a, b; while(in.nextToken() != StreamTokenizer.TT_EOF) { a = (int)in.nval; in.nextToken(); b = (int)in.nval; out.println(a + b); } out.flush(); } }To read and write ASCII characters with codes more than 127 in the same way as in other programming languages, you should use the following constructors: Scanner scanner = new Scanner(System.in, "ISO-8859-1"); BufferedReader reader = new BufferedReader(new InputStreamReader(System.in, "ISO-8859-1")); PrintWriter writer = new PrintWriter(new OutputStreamWriter(System.out, "ISO-8859-1"));It should be done since the default server charset does not guarantee a proper input in this case. If a default charset is used, some characters with codes from 128 to 255 might be converted to Unicode characters with codes more that 255. And then such characters may be printed as, for example, question marks. A Standard stack size for Java programs is quite small (less than 256 KB). To increase the maximum stack size Java programs are executed on the server with the following command line arguments:
java -Xss64m YourClassNameIt allows all the program threads to use up to 64 MB stack. Used stack size is considered while calculating a total used memory space. Here is an example of a proper usage of this feature (this program does not require a large stack, and the code just shows how to use threads): import java.io.*; import java.util.*; public class Main implements Runnable { void solve() throws IOException { int a = nextInt(); int b = nextInt(); out.println(a + b); } StreamTokenizer in; PrintWriter out; int nextInt() throws IOException { in.nextToken(); return (int)in.nval; } public void run() { try { in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); out = new PrintWriter(new OutputStreamWriter(System.out)); solve(); out.flush(); } catch (IOException e) { throw new IllegalStateException(e); } } public static void main(String[] args) { new Thread(new Main()).start(); } } |
||||||||||
|