高速入出力つきの雛形。本体で読み、出力はまとめて最後に。これをコピーして使う。
import java.io.*;
import java.util.*;
public class Main {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer st = new StringTokenizer("");
static String next() throws IOException {
while (!st.hasMoreTokens()) st = new StringTokenizer(br.readLine());
return st.nextToken();
}
static int nextInt() throws IOException { return Integer.parseInt(next()); }
static long nextLong() throws IOException { return Long.parseLong(next()); }
public static void main(String[] args) throws IOException {
StringBuilder sb = new StringBuilder();
// ... 読む ...
System.out.print(sb); // まとめて出力
}
}
1 個の整数、1 行に空白区切りで複数の整数。
入力 5 3 7 2
int n = nextInt(); // 1 個 int a = nextInt(), b = nextInt(), c = nextInt(); // 1 行に複数
N を読み、続けて N 個の数列。next/cin は空白・改行どちらの区切りも吸収する。
入力 読み取り 3 n = 3 1 2 3 a = [1, 2, 3]
int n = nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt();
N 行それぞれに複数の値。グラフの辺入力など。
入力 3 1 2 2 3 1 3
int n = nextInt();
int[][] e = new int[n][2];
for (int i = 0; i < n; i++) { e[i][0] = nextInt(); e[i][1] = nextInt(); }
H W を読み、続けて H 行の文字列。grid[y][x] でアクセス。
入力 2 3 .#. ..#
int h = nextInt(), w = nextInt(); char[][] grid = new char[h][]; for (int i = 0; i < h; i++) grid[i] = next().toCharArray();
空白で区切られた 1 語と、空白を含む 1 行全体の読み分け。
String s = next(); // 空白区切りの 1 語 String line = br.readLine(); // 1 行丸ごと
クエリ回数 T が先頭にある形と、ICPC 系の「0 で終端」する形。
int t = nextInt();
while (t-- > 0) { /* 1 ケース */ }
// ICPC: 0 終端
while (true) { int n = nextInt(); if (n == 0) break; /* ... */ }
配列をスペース区切り、小数の桁指定、大量出力はまとめて。
for (int i = 0; i < n; i++) // 配列をスペース区切り sb.append(a[i]).append(i + 1 < n ? ' ' : '\n'); System.out.printf("%.10f%n", ans); // 小数 10 桁