The use of JAVA in acm/various online written questions __java

Source: Internet
Author: User

First, the Java ACM attention Point

1. Class names must be named with public class main

2. On some OJ systems, even if the end of the output is a "", the program may output errors, so in my opinion a lot of OJ system do is very rubbish

3. Some OJ topics will directly copy the topics on the Oi, so even if there are input and output files in the topic, it may not be necessary because in the OJ system the standard input output is generally used, and no documentation is required

4. In cases where there are multiple lines of data input, this is generally done,

[Java] view plain copy static Scanner in = new Scanner (system.in); while (In.hasnextint ()) or a while (In.hasnext ())
5. For the use of the System.nanotime () function, this function is used to return the most accurate current value of the available system timer, in nanoseconds.

[Java] view plain copy long starttime = System.nanotime (); ... the code being measured. Long estimatedtime = System.nanotime ()-starttime;

Second, the Java input and output processing

Because the ACM Competition topic input data and output data generally have many groups (indefinite), and the format is various, so, how to deal with the problem of input and output is a most basic requirement for everyone. This is also a big problem for beginners.

1. Input:

Format 1:scanner sc = new Scanner (new Bufferedinputstream (system.in));

Format 2:scanner sc = new Scanner (system.in);

Format 1 is faster when you read a large amount of data.

Read an integer: int n = sc.nextint (); Equivalent to scanf ("%d", &n); or CIN >> N;

Read a string: string s = Sc.next (); Equivalent to scanf ("%s", s); or CIN >> s;

Read a floating-point number: double t = sc.nextdouble (); Equivalent to scanf ("%lf", &t); or cin >> T;

Read a whole line: String s = sc.nextline (); Equivalent to gets (s); or Cin.getline (...);

Determine if there is a next input that can be sc.hasnext () or sc.hasnextint () or sc.hasnextdouble () or Sc.hasnextline ()

Example 1: Reading an integer

[Java] view plain copy input data has multiple groups, one row for each group and an integer.   Sample Input 123 Import Java.util.Scanner;   public class Main {public static void main (string[] args) {Scanner sc =new Scanner (system.in);   while (Sc.hasnext ()) {//judge whether to end int score = Sc.nextint ();//Read int .... }   }   }

Example 2: Reading into real numbers

There are multiple sets of input data, each group of 2 rows, the first behavior an integer n, indicating that the second row contains n real numbers.

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.