JAVA快速输入输出模板
简介
在算法竞赛中,JAVA的Scanner和System.out.println速度太慢,我们需要使用一种更快的方式来输入输出
代码模板
import java.io.*;
import java.util.*;
public class Main{
public static BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
public static PrintWriter stdOut = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
public static void main(String[] args) throws IOException{
}
}
用法
情形1
样例输入如下:
1
2
3
4
我们可以使用如下代码:
for(int i = 0; i < n; i++)
a[i] = Integer.parseInt(stdIn.readLine().trim());
情形2
样例输入如下:
1 2 3 4 5 6 7
我们可以使用如下代码:
String[] sp = stdIn.readLine().trim().split(" ");
for(int i = 0; i < n; i+++)
a[i] = Integer.parseInt(sp[i]);
情形3
在输出数据的时候我们可以使用stdOut.println()或stdOut.print()
PS:如果用到了快速输出,则需要在程序的最后加上stdOut.flush(),否则不会输出任何东西!
特别提醒
如果用到了快速输入输出,需要使用import java.io.*; ,而且不要忘记在main函数后加上throws IOException