Fast reading and fast writing necessary for java algorithm competition (super detailed interpretation)

Fast reading and fast writing necessary for java algorithm competition (super detailed interpretation)

Disadvantages of java writing algorithm: slow speed, complex reading and writing, inexplicable WA (wrong answer), TL (timeout), CL (super memory)...

(Then what else should we learn? It’s not good to switch to C++ to write algorithms.) Don’t worry, don’t worry, there are advantages if there are disadvantages.

We don't know if it smells good or not. But we can't afford to play c++. There are too many bosses, too curly.

So the advantages of java are reflected.

The advantages of writing algorithms in java: fewer people can win prizes in competitions, as a veteran language in the web and Android fields, java is still a father-level existence, so writing algorithms in java can also prepare for future development. . .

No more nonsense, just read and write quickly.

StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));//快读
PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));//快写

How to use: Here is a super convenient method to use, directly write a read class

class Read{
    
    
	StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
	public int nextInt() throws Exception{
    
    
		st.nextToken();
		return (int)st.nval;
	}
	public String readLine() throws Exception{
    
    	//	不推荐使用
		st.nextToken();
		return st.sval;
	}
}

The following demonstrates the use of this reading class

package demo;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;

public class Main {
    
    
	public static void main(String[] args) throws Exception {
    
    
		PrintWriter p = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
		Read r = new Read();
		int a = r.nextInt();
		String str1 = r.readLine();
		int b =r.nextInt();
		String str2 = r.readLine();
		System.out.println(a+str1+b+str2);
	}
}
class Read{
    
    
	StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
	public int nextInt() throws Exception{
    
    
		st.nextToken();
		return (int)st.nval;
	}
	public String readLine() throws Exception{
    
    	//	这个不推荐使用
		st.nextToken();
		return st.sval;
	}
}

enter

32
adfa
23
AAA

output

32adfa23AAA55

It is known from the input and output that the values ​​are all read in.

Notice

When we use StreamTokenizer, we generally only use it to process digital input. For strings, we do not use the above st.sval, that is, the above readLine() method, because st.sval can only read letters, but not special symbols and numbers. Fetch, read in will be null.

So if we encounter reading strings, we still use BufferedReader honestly.

BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
bf.readLine();

Well, after reading this article you can forget about Scanner. I will not give an example here to compare the reading time of Scanner, BufferedReader, and StreamTokenizer. You just need to remember that BufferedReader and StreamTokenizer are far faster than Scanner. So let's just forget about him. I know you may not want to. But it's a sign of our progress. The certificate is attached at the end, it is still useful to read it quickly, hahaha.
Please add a picture description

おすすめ

転載: blog.csdn.net/gudada010/article/details/117526186