Java in ACM/ICPC

Source: Internet
Author: User

Directory

    • The features of Java in ACM/ICPC

    • Issues to be aware of when using Java in ACM/ICPC

    • Java and high-precision computing

Features of the 1.Java in ACM/ICPC

Java syntax is almost the same as C + +

Java is not much slower than C + + in performing compute-intensive tasks, just slower IO operations

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

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

Java is not easy to make subtle mistakes

A pointer in C + +

"if (n = m) ... ”

Java and Eclipse

2. Issues to be aware of when using Java in ACM/ICPCJava Program Structure

Java I/O

JDK1.5.0 new Scanner class is well suited for AMC/ICPC input

General steps for using the Scanner class

1. Import the Scanner class

Import Java.util.Scanner;

2. Creating an object of the scanner class

Scanner cin=new Scanner (system.in); Read data from standard input

Scanner cin=new Scanner ("12 30")); Reading data from a string

3. Read all types of data using objects of the scanner class

Cin.nextint ()

Cin.nextdouble ();

...

Common methods of the scanner Class 1. Read data

Scanner class method

corresponding to C operation

Corresponds to 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 if there is any data

Cin.hasnext () or cin.hasnextint () or cin.hasnextdouble ()

Examples of methods for scanner classes:

import java.util.*;  Public class main{    publicstaticvoid  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 break System.out.println (...); Output line break
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          // output multiple integers on the same line can be used        Cout.println (n+ "" +m);    }}

Controlling floating-point number decimal digits with the DecimalFormat class

Import Java.text.DecimalFormat;

Control method

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

The arguments in the constructor are pattern strings, 0 refers to one digit, #指除0以外的数字

Use the Decimaformat object to format the floating-point number that needs to be output: System.out.println (F.format (12.1234));

DecimalFormat Example

import java.text.*;  Public class decimalformat{    publicstaticvoid  main (string[] args) {          New DecimalFormat ("#.00#");         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));}    }

Operation Result:

123.451

123.451

.12

0.120

Another way to format the output is to use the System.out.printf ("format string",...), whose usage is basically consistent with the C printf
int a=10; float b=2.35f; System.out.printf (
Strings (String)

Common methods of the string class:

Construct the string:
String s="ABCDE"; Char [] chs={' A ', ' B ', ' C ', ' d ', ' e '}; string s=new string (CHS);
Get a character in a string:
Char Ch=s.charat (1);    // ch= ' B ';
Seek substring:
// output "abc" // output "BC" // output "BCDE"
Split string:
String s= "123:34:55= s.split (": ");  for (int i=0;i<ss.length;i++) System.out.println (Ss[i]);

Operation Result:

123

34

55

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

The characters in string cannot be changed and can be used if changes are required StringBuffer

Other matters to note

A Java array is an object that must be initialized after definition, such as int[] a = new int[100]; Array lengths are obtained by length members, such as SYSTEM.OUT.PRINTLN (A.length);

The Arrays class provides some useful methods:

Arrays.fill ()

Arrays.sort ()

Arrays.binarysearch ()

The Boolean type is Boolean, with only true and false two values, which must be a Boolean type in the conditions of the IF (...)/while (...) statement.

if (n% 2) in C + + ... cannot be compiled in Java.

Java also provides an STL-like collection class:

Vector,arraylist,map,queue,stack,hashtable

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

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

Methods for solving in C + +

The POW function is not able to achieve the required precision

Use arrays to simulate multiplication for improved accuracy

Java code:
Importjava.math.*;ImportJava.util.*; Public classmain{ Public Static voidMain (string[] args) {Scanner in=NewScanner (system.in);  while(In.hasnext ()) {BigDecimal Val=In.nextbigdecimal (); intn=In.nextint (); BIGDECIMAL ret=Val.pow (n). Striptrailingzeros (); System.out.println (Ret.toplainstring (). ReplaceAll ("^0", "" ") ); }    }}

BigDecimal class

High-precision signed decimal number constructs 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 methods of the BigDecimal class:
BigDecimal Add (BigDecimal augend)//"+"BigDecimal Subtract (BigDecimal subtrahend)//"-"BigDecimal Multiply (BigDecimal multiplicand)//"*"BigDecimal Divide (BigDecimal divisor)//"/"BigDecimal remainder (BigDecimal divisor)//"%"BigDecimal Pow (intN//"exponentiation"String toplainstring ()//returns a string representation without an exponentString toString ()//returns a string representation with an exponent
PKU High-precision calculation topics:

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

Java in ACM/ICPC

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.