Java in ACM/ICPC, javainacmicpc

Source: Internet
Author: User

Java in ACM/ICPC, javainacmicpc

Directory

  • Features of Java in ACM/ICPC

  • Notes for using Java in ACM/ICPC

  • Java and high-precision computing

1. Features of Java in ACM/ICPC

The syntax of Java is almost the same as that of C ++.

Java is not much slower than C/C ++ in executing computation intensive tasks, but it is only slow in I/O operations.

Java is simple and powerful, and some things are more convenient to implement using Java

For example: input and output, string parsing, and high precision

Java is not easy to make minor mistakes

Pointer in C/C ++

"If (n = m )..."

Java and Eclipse

2. Notes for using Java in ACM/ICPC java program structure

Java I/O

The classes Class added by JDK1.5.0 is suitable for input of AMC/ICPC.

General steps for using the category class

1. Import the category class

Import java. util. collections;

2. Create objects of the category class

Cin = new partition (System. in); // read data from standard input

Scanner cin = new bytes ("12 30"); // reads data from a string

3. Read data of various types using objects

Cin. nextInt ()

Cin. nextDouble ();

...

Common Methods of Reading classes 1. Reading data

CATEGORY Method

Corresponding C operation

Corresponding C ++ operations

Int n = cin. nextInt ();

Scanf ("% d", & n );

Cin> n;

String s = cin. next ();

Scanf ("% s", s );

Cin> s;

Double t = cin. nextDouble ();

Scanf ("% lf", & t );

Cin> t;

String s = cin. nextLine ();

Gets (s );

Cin. getline (...);

BigDecimal B = cin. nextBigDecimal ();

   
2. Determine whether data exists.

Cin. hasNext () or cin. hasNextInt () or cin. hasNextDouble ()

Usage example of the category class:

import java.util.*;public class Main{    public static void main(String[] args){        Scanner sc=new Scanner(System.in);        while(sc.hasNext()){            char ch=(char)sc.nextInt();            System.out.print(ch);        }     }}
Standard output System. out. print (...); // Do not output line feed System. out. println (...); // Output line feed
Import java. io. *; public class Main {static PrintStream cout = System. out; public static void main (String [] args) {int n = 3, m = 5; cout. println (n); // output 3 // multiple integers can be output in the same row using cout. println (n + "" + m );}}

Use the DecimalFormat class to control the number of decimal places of Floating Point Numbers

Import java. text. DecimalFormat;

Control Method

Construct a specific DecimalFormat object: DecimalFormat f = new DecimalFormat ("#. 00 #");

The parameter in the constructor is a pattern string. 0 indicates a digit and # indicates a number other than 0.

Format the floating point number to be output using the DecimaFormat object: System. out. println (f. format (12.1234 ));

DecimalFormat example

import java.text.*;public class decimalformat{    public static void main(String[] args){        DecimalFormat f = new DecimalFormat("#.00#");        DecimalFormat g = new DecimalFormat("0.000");        double a = 123.4509, b = 0.12;        System.out.println(f.format(a));        System.out.println(g.format(a));        System.out.println(f.format(b));        System.out.println(g.format(b));    }}

Running result:

123.451

123.451

. 12

0.120

Another way to format the output is to use System. out. printf ("Format String ",...), The method and c printf are basically the same.
int a=10;float b=2.35f;System.out.printf("%d %10.5f\n", a, b); 
String)

Common Methods of the String class:

Constructor string:
String s=“abcde”;char[] chs={‘a’,’b’,’c’,’d’,’e’};String s=new String(chs);
Obtain a character in a string:
char ch=s.charAt(1);    //ch=‘b’;
Substring:
System.out.println(s.substring(0, 3)) // output “abc"System.out.println(s.substring(1, 3)) // output “bc"System.out.println(s.substring(1)) // output “bcde"
Split string:
String s=“123:34:55”;String[] ss = s.split(“:”);for(int i=0;i<ss.length;i++) System.out.println(ss[i]);

Running result:

123

34

55

Replacement string:
String s = ""; System. out. println (s. replace ('-', '//'); // output 2009/07/26 String s = "0.123456"; System. out. println (s. replaceAll ("^ 0", ""); // output. 123456

The characters in the String cannot be changed. You can use StringBuffer if needed.

Other considerations

The Java array is an object and must be initialized after definition, such as int [] a = new int [100]. The length of the array is obtained by the length member, such as System. out. println (. length );

The Arrays class provides some useful methods:

Arrays. fill ()

Arrays. sort ()

Arrays. binarySearch ()

The boolean type is boolean, which has only two 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.

Java also provides collection classes similar to STL:

Vector, ArrayList, Map, Queue, Stack, Hashtable

3. Java and high precision computing PKU1001-exponentiation (Power ):

Sample Input

95.123 12

0.4321 20

5.1234 15

6.7592 9

98.999 10

1.0100 12

Sample Output

548815620517731830194541.899025343415715973535967221869852721

. 00000005148554641076956121994511276767154838481760200726351203835

429763013462401

43992025569.928573701266488041146654993318703707511666295476720493

953024

29448126.764121021618164430206909037173276672

90429072743629540498.107596019456651774561044010001

1.126825030131969720661201

Solution Using C/C ++

The pow function of C/C ++ cannot achieve the required precision.

C/C ++ uses arrays to simulate multiplication and Improve Accuracy

Java code:
import java.math.*;import java.util.*;public class Main{    public static void main(String[] args){        Scanner in=new Scanner(System.in);        while(in.hasNext()){            BigDecimal val=in.nextBigDecimal();            int n=in.nextInt();            BigDecimal ret=val.pow(n).stripTrailingZeros();            System.out.println( ret.toPlainString().replaceAll("^0", "") );        }    }}

BigDecimal class

A high-precision signed decimal number to construct a high-precision number

BigDecimal (int val)

BigDecimal (double val)

BigDecimal (String val)

BigDecimal d1=new BigDecimal(1);BigDecimal d2=new BigDecimal(0.1);BigDecimal d3=new BigDecimal("0.1");        System.out.println("d1="+d1);System.out.println("d2="+d2);System.out.println("d3="+d3);
Common BigDecimal class methods:
BigDecimal add (BigDecimal augend) // "+" BigDecimal subtract (BigDecimal subtrahend) // "-" BigDecimal multiply (BigDecimal multiplicand) // "*" BigDecimal divide (BigDecimal divisor) // "/" BigDecimal remainder (BigDecimal divisor) // "%" BigDecimal pow (int n) // "power" String toPlainString () // returns a String that does not contain an index to indicate String toString () // returns an indexed String.
PKU High Precision computing questions:

1131, 1205, 1220, 1405, 1503, 1604, 1894, 2084, 2305, 2325, 2389, 2413, 3101

 
JAVA learning is suitable for acm

I think there is no problem.

I learned java myself.
I have also participated in ICPC competitions of ACM.
I also won the first prize in the ACM/ICPC Programming Competition of the Inner Mongolia Autonomous Region a few days ago.

I think java is better suited to the ACM competition than C ++ for the following reasons:
1. java is a compilation + Interpretation Language. After compilation, the speed is not much slower than that of C ++ and C. After my competition experience, ACM is as fast as the program written in C ++, and can solve the problem within the required Ms.
2. The classes provided by java are powerful and efficient, which will make the problem solving faster (for example, a question requires forward input and reverse output. This is a simple question. Because the test data is very huge, the input of this question using the C ++ io stream will time out, but C and java won't .). The container class is also very useful.
3. Currently, only domestic competitions are supported! Use C ++ and C. The real international competition (the Global Competition of IBM) is said to have been submitted only in java ....
4. in domestic competitions, some will use the linux + eclipse environment competition. eclipse was originally developed for java (and developed using java). The eclipse environment is quite effective for editing java programs.

If it is too far away, one or two items are the most important.

We had a question during the last week's competition. The idea of this question was to sort and remove duplicates.
The contestants in our competition basically used the QuickSort + two-layer FOR structure except FOR duplicates. However, we compiled the code in java and finally passed the test, the program compiled by C ++ has timed out multiple times .... I am confused now ....

Conclusion: I wish my friends who asked questions a good cheer and a good result.
Thank you.
 
ACM/ICPC rules

First blood, the first drop of blood. dota has played it. Here, we have a question. The first one in a team is done first. It is absolutely correct. I am an inner.

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.