我们在使用java写算法题时,常常会因为超时而苦恼,明明是和C++一样的思路,甚至代码实现都极为相似,但是却比它慢上一大截,这时候不妨试试IO流读入输出数据,提升速度。
参考文章
Java写算法题中那些影响你效率的细节(关于暴力破解算法题的细节处理)
Java在算法竞赛中的一些技巧
Java算法竞赛常用模板
我的模板
package 算法竞赛;
import java.io.*;
public class Test1{
// 重点:在洛谷中BufferedReader和StreamTokenizer在同一个程序中只能使用一个,否则会出现不能够正常读取数据的情况
// 其他的刷题网站不确定。
static BufferedReader ins = new BufferedReader(new InputStreamReader(System.in));
// ins 用于读入字符串这样的数据,当然也可以读入数字等,不过是String转变为基本类型数据
static StreamTokenizer in = new StreamTokenizer(ins);
// in 用于读入int、long、double等基本数据类型,当然也可以用来读入String(有一定的限制条件)
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
// out 用于输出,代替以前的System.out.println();
public static void main(String[] args) throws IOException {
int a = 0; // 不要忘记在main函数上抛异常
int b = 0;
in.nextToken(); // 读入数据时,记得每一次读取,都要先in.nextToken()
a = (int)in.nval; // 因为in.nval接收的是double类型,所以要强制类型转换
in.nextToken();
b = (int)in.nval;
out.println("a + b = "+(a+b));
// 此时输入:hello world!
in.nextToken();
String s1 = in.sval; // 类似于以前的Scanner的next()方法。
in.nextToken(); // 同时也不要忘了in.nextToken()!!!
String s2 = in.sval;
out.println(s1); // hello
out.println(s2); // world
// 细心就会发现,为什么读入的是world,而不是world!
// 其实这就需要看另一篇文章了,参考文章见下方。
// 此时输入:你好,世界! // 因为上面输入的是:hello world!所以,下面就还没输入,程序就结束了
String s = ins.readLine(); // 而输入:hello world 就可以正常进行。
// 如果要用ins.readLine()读取数字
// 此时输入类似于 (int)数字 (int)数字
// 根据题目,我们应该知道输入几个数字,和他们的类型,并根据这些读入读入数据。
String string = ins.readLine();
String[] strings = string.split(" ");
int sa = Integer.parseInt(strings[0]); // 类似的String转变为Integer方法见下方的图片
int st = Integer.parseInt(strings[1]);
out.println(sa);
out.println(st);
out.println(s); // 所以sval用的时候一定要谨慎考虑输入的数据是否会影响后面数据的读入!!
out.close(); // out.close()一定不能忘!否则不输出数据
}
}
原文:Java string和各种格式互转 string转int int转string

模板测试
输入样例
1 1
hello world
你好,世界
2 2
输出样例
a + b = 2
hello
world
2
2
你好,世界
sval用法:
参考文章:初探java.io.StreamTokenizer类
测试
大家可以找个刷题网站测试,这里以洛谷为例:P1001 A+B Problem
用平常的Scanner读入,和用我这套模板读入来测试二者时间相差多少。
这里给出我的测试代码
StreamTokenizer 读取:
import java.io.*;
public class Main{
static BufferedReader ins = new BufferedReader(new InputStreamReader(System.in));
static StreamTokenizer in = new StreamTokenizer(ins);
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
public static void main(String[] args) throws IOException {
in.nextToken();
int a = (int)in.nval;
in.nextToken();
int b = (int)in.nval;
out.print(a+b);
out.close();
}
}
用时:207ms
BufferedReader 读取:
import java.io.*;
public class Main{
static BufferedReader ins = new BufferedReader(new InputStreamReader(System.in));
static StreamTokenizer in = new StreamTokenizer(ins);
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
public static void main(String[] args) throws IOException{
String str = ins.readLine();
String[] strings = str.split(" ");
int a = Integer.parseInt(strings[0]);
int b = Integer.parseInt(strings[1]);
out.print(a+b);
out.close();
}
}
用时:241ms
Scanner 读取:
package 算法竞赛;
import java.util.Scanner;
public class Test1{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int a = in.nextInt();
int b = in.nextInt();
System.out.println(a+b);
}
}
用时:361ms
总结:能用StreamTokenizer就用它,不过要注意sval能否正确读取数据,可以先测试下,否则选择BufferedReader,但是Scanner绝对要避免使用它。
如有谬误,请务必告知,以免误导他人
如果各位看官觉得写得不错,对你有帮助,能不能给个赞啊,这是对我最大的鼓励!