Application of Java in ACM and matters needing attention

Application of Java in ACM and matters needing attention

Original link: https://www.xuebuyuan.com/3257862.html

0, basic framework

import java.oi.*;

import java.util.* 

public class Main 

{
    
     

public static void main(String args[]) 

{
    
     

Scanner cin = new Scanner(System.in));

} 

} 

1. Read in data until the end of the file

while (in.hasNext()) {
    
    
	int n = in.nextInt();
	System.out.println(n);
}

2. Java input

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 a whole line: String s = cin.nextLine();

Equivalent to gets(s); or cin.getline(...);

To determine whether there is the next input, use cin.hasNext() or cin.hasNextInt() or cin.hasNextDouble()

3. Java output

The output can generally be directly used System.out.print() and System.out.println(), the former does not output a newline, and the latter outputs.

String name = s.nextLine();  
int age = s.nextInt();  
System.out.println("姓名:" + name + "  年龄:" + age );  
System.out.println();   //换行

4. Floating point output

import java.text.*; 

public class Main

{
    
    

public static void main(String[] args)

{
    
    

DecimalFormat g = new DecimalFormat("0.00"); 

double a = 123.45678;

System.out.println(g.format(a)); 

}

}

//输出为123.46

5. Java's high precision

import java.math. // 需要引入 java.math 包
BigInteger a = BigInteger.valueOf(100);
BigInteger b = BigInteger.valueOf(50);
BigInteger c = a.add(b) // c = a + b;
//主要有以下方法可以使用:
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)
*

Remember:

The BigInteger class cannot perform operations such as ±, and must call its member functions to perform operations.

In addition, the BigInteger class is not an extended type of int, so the int type value cannot be directly assigned to BigInteger.

The BigInteger type has its own constants:

BigInteger.zero

BigInteger.one

6, Java string

The String class is used to store strings. You can use the charAt method to take out one of the bytes, and the count starts from 0:

String a = “Hello”; // a.charAt(1) = ‘e’

Use the substring method to get the substring, as in the above example

System.out.println(a.substring(0, 4)) // output “Hell”

Note that the character in the second parameter position is not included. Doing so makes s.substring(a, b) always have ba characters.

String concatenation can directly use the + sign, such as

String a = “Hello”;
String b = “world”;
System.out.println(a + ", " + b + “!”); // output "Hello, world!“

It is not possible to change a single character through the charAt method

7, Java array

The definition of the array:

int[] a = new int[100];

Initialization of the array

Arrays.fill(a,0);

It is equivalent to memset(a,0,sizeof(a)) in C language;

8. Some notes

When using Netbeans to write Java programs, automatically add package main; this sentence should be removed when submitting questions.

The main class must be named Main

Although Java is very powerful, you cannot rely on it completely. After all, C/C++ is still the mainstream language of ACM/ICPC. Some questions will time out no matter how you write them in Java, you can use Java to calculate the results and submit them in C/C++.

When submitting questions, your code should be the following frame

import ………//相当于c++的include

public class Main {
    
    

    public static…….//一些自己定义的函数

    public static void main(String[] args) {
    
    

        Scanner cin = new Scanner(System.in);

    }

}

The syntax of Java is still very similar to c/c++, so. . Feel free to write. .

/*

Java input acceleration

用BufferedReader和StringTokenizer代替Scanner
   */
    
    import java.io.*;
    
    import java.util.*;
    
    public class Main {
    
    
    
        public static void main(String[] args) throws IOException {
    
    
    
         Reader Reader =new Reader(System.in);
    
         double x = Reader.nextDouble();
    
         int n = Reader.nextInt();
    
         String str = Reader.next();
    
        }
    
    }
    
    class Reader {
    
    
    
        final BufferedReader reader;
    
        StringTokenizer tokenizer;
    
        public Reader(InputStream input) {
    
    
    
            reader = new BufferedReader(new InputStreamReader(input));
    
            tokenizer = new StringTokenizer("");
    
        }
    
        public String next() throws IOException {
    
    
    
            while (!tokenizer.hasMoreTokens())
    
                tokenizer = new StringTokenizer(reader.readLine());
    
            return tokenizer.nextToken();
    
        }
    
        public int nextInt() throws IOException {
    
    
    
            return Integer.parseInt(next());
    
        }
    
        public double nextDouble() throws IOException {
    
    
    
            return Double.parseDouble(next());
    
        }
    
    }

Guess you like

Origin blog.csdn.net/qq_40534166/article/details/99346591