Java ACM Basics

Source: Internet
Author: User

Switch from csdn

Java ACM

Java do ACM-ICPC features:

(1) In general competitions, Java programs have extra time and space. In fact, after experiments, Java is not much slower than C/C ++ when executing computing intensive tasks, i/O operations are slow.
(2) Java is simple and powerful, and some things are more convenient to implement using Java, such as high precision.
(3) using Java is not easy to make minor errors, such as pointers in C/C ++ and "If (n = m )...".
(4) Currently, eclipse has become a basic configuration. Writing a Java program is easier to debug than C/C ++. It is also an option for a specific competition.
(5) Learning Java is good for future work. At present, there are more java users in many foreign countries than C/C ++ users.

The following describes some problems encountered by the ACM-ICPC team in Java programming:

1. Basic Input and Output:

(1)
JDK 1.5.0 new classes provide a good foundation for input, it is set for ACM-ICPC.

General Usage:

Import java. Io .*
Import java. util .*

Public class main
{
Public static void main (string ARGs [])
{
Partition CIN = new partition (New bufferedinputstream (system. In ));
...
}
}

Of course, you can also directly convert CIN = new partition (system. In );
But adding a 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 the entire row: String S = cin. nextline (); equivalent to gets (s); or CIN. Getline (...);
You can use cin. hasnext (), Cin. hasnextint (), or CIN. hasnextdouble () to determine whether there is a next input. For details, see the toJ 1001 routine.

(3)
Generally, system. Out. Print () and system. Out. println () can be used for output directly. The former does not output line breaks, while the latter does.
For example: system. Out. println (n); // n is int type
Multiple integers can be output in the same row.
System. Out. println (New INTEGER (N). tostring () + "" + new INTEGER (M). tostring ());

You can also redefine it:

Static printwriter cout = new printwriter (New bufferedoutputstream (system. Out ));
Cout. println (N );

(4)
You can use the decimalformat class to retain a few decimal places in the output floating point,

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 ());
System. Out. println (F. Format (B ));
System. Out. println (G. Format (B ));

Here 0 refers to a digit, and # refers to a Number other than 0.

2. Large numbers

Biginteger and bigdecimal are existing classes in the Java. Math package. The former represents an integer, and the latter represents a floating point.

Usage:
You cannot directly use symbols such as + or-to use large numbers. For example:

(Import java. Math. *) // you need to introduce the java. Math package.

Biginteger A = biginteger. valueof (100 );
Biginteger B = biginteger. valueof (50 );
Biginteger c = A. Add (B) // C = A + B;

You can use the following methods:
Biginteger add (biginteger other)
Biginteger subtract (biginteger other)
Biginteger multiply (biginteger other)
Biginteger divide (biginteger other)
Biginteger Mod (biginteger other)
Int compareto (biginteger other)
Static biginteger valueof (long X)

Use System. Out. println (A) to output a large number.

3. String

The string class is used to store strings. You can use the charat method to retrieve a certain byte. The count starts from 0:

String A = "hello"; // A. charat (1) = 'E'

Use the substring method to obtain the substring.

System. Out. println (A. substring (0, 4) // output "hell"

Note that the characters at the 2nd parameter locations are not included. In this way, S. substring (a, B) always contains B-A characters.

You can use the plus sign (+) for string connection, as shown in figure

String A = "hello ";
String B = "world ";
System. Out. println (a + "," + B + "! "); // Output" Hello, world! "

If you want to directly change a byte in the string, you can use another stringbuffer class.

4. Call recursion (or other dynamic methods)

In the main class, the main method must be public static void, and a warning will be generated when a non-static class is called in main,
You can first create an object and then call the method through the object:

Public class main
{
...

Void DFS (int)
{
If (...) return;
...
DFS (a + 1 );
}

Public static void main (string ARGs [])
{
...
Main E = new main ();
E. DFS (0 );
...
}
}

5. Other Precautions

(1) Java is an object-oriented language, and the thinking methods need to be transformed. The functions in it are collectively referred to as methods. Do not make mistakes.

(2) there are some changes to the array in Java. In fact, the internal structure of multi-dimensional arrays is pointers. Therefore, Java does not support fill multi-dimensional arrays.
The array must be initialized after definition, for example, int [] A = new int [100];

(3) The boolean type is Boolean and only has the values true and false. It must be boolean in the conditions of IF (...)/while (...) statements.
In C/C ++, if (N % 2)... cannot be compiled in Java.

(4) The following methods of the arrays class in the Java. util package can replace memset, qsort/sort, and bsearch in C/C ++:

Arrays. Fill ()
Arrays. Sort ()
Arrays. binarysearch ()

Related Article

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.