ACM- java在acm中的快速读入,多组读入,快速输出模板


代码

多组读入a+b problem问题

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.Scanner;

public class Main {
	public static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
	public static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));

	public static int nextInt() throws IOException {
		in.nextToken();
		return (int) in.nval;
	}

	public static String next() throws IOException {
		in.nextToken();
		return (String) in.sval;
	}

	public static double nextDouble() throws IOException {
		in.nextToken();
		return in.nval;
	}

	public static float nextFloat() throws IOException {
		in.nextToken();
		return (float) in.nval;
	}

	public static void main(String[] args) throws IOException {
		while (in.nextToken() != StreamTokenizer.TT_EOF) {
			int a = (int) in.nval;// 第一个读入的int要用nval成员变量
			int b = nextInt();
			out.println(a + b);
			out.flush();
		}
		out.close();
	}
}

多组输入字符串

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.Scanner;

public class Main {
	public static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
	public static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));

	public static int nextInt() throws IOException {
		in.nextToken();
		return (int) in.nval;
	}

	public static String next() throws IOException {
		in.nextToken();
		return (String) in.sval;
	}

	public static double nextDouble() throws IOException {
		in.nextToken();
		return in.nval;
	}

	public static float nextFloat() throws IOException {
		in.nextToken();
		return (float) in.nval;
	}

	public static void main(String[] args) throws IOException {
		while (in.nextToken() != StreamTokenizer.TT_EOF) {
			String a = (String) in.sval;// 第一个读入的String要用sval变量
			String b = next();
			out.println(a + " " + b);
			out.flush();
		}
		out.close();
	}
}

StreamTokenizer有个很蛋疼的地方,比如读入asd123,它会把字母和数字分隔开
遇到字母数字混合出现的情况,建议还是用以下方法,也快于Scanner

	BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
	String[] str = reader.readLine().split(" ");
	int n = Integer.parseInt(str[0]);
	int m = Integer.parseInt(str[1]);