はじめに
競プロで使う Java のチートシートを載せています。
標準入力
記号は次の意味で使い分けています。
整数:$N, M, A_i, B_i, H, W$
文字列:$S, T, S_i$
コメントで記載されている部分に入るコードのチートシートになります。
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// ここの部分を載せています
}
}
標準入力が int 型の場合
$N$
int n = sc.nextInt();
$N$ $M$
int n = sc.nextInt(); int m = sc.nextInt();
$N$
$A_1$ $A_2$ $\dots$ $A_N$int n = sc.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); }
$N$
$A_1$
$A_2$
$\vdots$
$A_N$int n = sc.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); }
$N$
$A_1$ $B_1$
$A_2$ $B_2$
$\vdots$
$A_N$ $B_N$int n = sc.nextInt(); int[] a = new int[n]; int[] b = new int[n]; for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); b[i] = sc.nextInt(); }
$H$ $W$
$A_{1,1}$ $A_{1,2}$ $\dots$ $A_{1,W}$
$A_{2,1}$ $A_{2,2}$ $\dots$ $A_{2,W}$
$\vdots$
$A_{H,1}$ $A_{H,2}$ $\dots$ $A_{H,W}$int h = sc.nextInt(); int w = sc.nextInt(); int[][] a = new int[h][w]; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { a[i][j] = sc.nextInt(); } }
標準入力が long 型の場合
$N$
long n = sc.nextLong();
$N$ $M$
long n = sc.nextLong(); long m = sc.nextLong();
$N$
$A_1$ $A_2$ $\dots$ $A_N$int n = sc.nextInt(); long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = sc.nextLong(); }
$N$
$A_1$
$A_2$
$\vdots$
$A_N$int n = sc.nextInt(); long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = sc.nextLong(); }
$N$
$A_1$ $B_1$
$A_2$ $B_2$
$\vdots$
$A_N$ $B_N$int n = sc.nextInt(); long[] a = new long[n]; long[] b = new long[n]; for (int i = 0; i < n; i++) { a[i] = sc.nextLong(); b[i] = sc.nextLong(); }
$H$ $W$
$A_{1,1}$ $A_{1,2}$ $\dots$ $A_{1,W}$
$A_{2,1}$ $A_{2,2}$ $\dots$ $A_{2,W}$
$\vdots$
$A_{H,1}$ $A_{H,2}$ $\dots$ $A_{H,W}$int h = sc.nextInt(); int w = sc.nextInt(); long[][] a = new long[h][w]; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { a[i][j] = sc.nextLong(); } }
標準入力が文字列の場合
$S$
String s = sc.next();
$S$ $T$
String s = sc.next(); String t = sc.next();
$N$
$S_1$
$S_2$
$\vdots$
$S_N$int n = sc.nextInt(); String[] s = new String[n]; for (int i = 0; i < n; i++) { s[i] = sc.next(); }
型変換
記号は次の意味で使い分けています。
$\text{int}$ 型の整数:$N_{\text{int}}$
$\text{long}$ 型の整数:$N_{\text{long}}$
文字:$C$
文字列:$S$
$\text{int}$ 型の整数の配列:$L_{N_\text{int}}$
$\text{long}$ 型の整数の配列:$L_{N_\text{long}}$
文字の配列:$L_C$
文字列の配列:$L_S$
型変換は期待通りの結果にならない場合があります。
例えば、 $S$ $\rightarrow$ $C$ では、文字列 $S$ の長さが $1$ 以外の場合は期待通りの結果になりません。
他にも、 $N_{\text{long}}$ $\rightarrow$ $N_{\text{int}}$ では、 $N_{\text{long}}$ の値が $32\, \text{bit}$ 整数型に収まらない場合は型変換を行うとオーバーフローします。
$N_{\text{int}}$ $\rightarrow$ $N_{\text{long}}$ $(\ \text{int}\ \rightarrow \text{long}\ )$
long n_long = n_int;
$N_{\text{long}}$ $\rightarrow$ $N_{\text{int}}$ $(\ \text{long}\ \rightarrow \text{int}\ )$
int n_int = (int) n_long;
$N_{\text{int}}$ $\rightarrow$ $C$ $(\ \text{int}\ \rightarrow \text{char}\ )$
char c = (char) n_int;
$N_{\text{long}}$ $\rightarrow$ $C$ $(\ \text{long}\ \rightarrow \text{char}\ )$
char c = (char) n_long;
$N_{\text{int}}$ $\rightarrow$ $S$ $(\ \text{int}\ \rightarrow \text{String}\ )$
String s = String.valueOf(n_int);
$N_{\text{long}}$ $\rightarrow$ $S$ $(\ \text{long}\ \rightarrow \text{String}\ )$
String s = String.valueOf(n_long);
$N_{\text{int}}$ $\rightarrow$ $L_{N_\text{int}}$ $(\ \text{int}\ \rightarrow \text{int [ ]}\ )$
char[] l_c = String.valueOf(n_int).toCharArray(); int[] l_n_int = new int[l_c.length]; for (int i = 0; i < l_c.length; i++) { l_n_int[i] = l_c[i] - '0'; }
$N_{\text{long}}$ $\rightarrow$ $L_{N_\text{long}}$ $(\ \text{long}\ \rightarrow \text{long [ ]}\ )$
char[] l_c = String.valueOf(n_long).toCharArray(); long[] l_n_long = new long[l_c.length]; for (int i = 0; i < l_c.length; i++) { l_n_long[i] = l_c[i] - '0'; }
$N_{\text{int}}$ $\rightarrow$ $L_C$ $(\ \text{int}\ \rightarrow \text{char [ ]}\ )$
char[] l_c = String.valueOf(n_int).toCharArray();
$N_{\text{long}}$ $\rightarrow$ $L_C$ $(\ \text{long}\ \rightarrow \text{char [ ]}\ )$
char[] l_c = String.valueOf(n_long).toCharArray();
$N_{\text{int}}$ $\rightarrow$ $L_S$ $(\ \text{int}\ \rightarrow \text{String [ ]}\ )$
String[] l_s = String.valueOf(n_int).split("");
$N_{\text{long}}$ $\rightarrow$ $L_S$ $(\ \text{long}\ \rightarrow \text{String [ ]}\ )$
String[] l_s = String.valueOf(n_long).split("");
$C$ $\rightarrow$ $N_{\text{int}}$ $(\ \text{char}\ \rightarrow \text{int}\ )$
int n_int = c;
$C$ $\rightarrow$ $N_{\text{long}}$ $(\ \text{char}\ \rightarrow \text{long}\ )$
long n_long = c;
$C$ $\rightarrow$ $S$ $(\ \text{char}\ \rightarrow \text{String}\ )$
String s = String.valueOf(c);
$S$ $\rightarrow$ $N_{\text{int}}$ $(\ \text{String}\ \rightarrow \text{int}\ )$
int n_int = Integer.parseInt(s);
$S$ $\rightarrow$ $N_{\text{long}}$ $(\ \text{String}\ \rightarrow \text{long}\ )$
long n_long = Long.parseLong(s);
$S$ $\rightarrow$ $C$ $(\ \text{String}\ \rightarrow \text{char}\ )$
char c = s.charAt(0);
$S$ $\rightarrow$ $L_{N_\text{int}}$ $(\ \text{String}\ \rightarrow \text{int [ ]}\ )$
int[] l_n_int = new int[s.length()]; for (int i = 0; i < s.length(); i++) { l_n_int[i] = s.charAt(i) - '0'; }
$S$ $\rightarrow$ $L_{N_\text{long}}$ $(\ \text{String}\ \rightarrow \text{long [ ]}\ )$
long[] l_n_long = new long[s.length()]; for (int i = 0; i < s.length(); i++) { l_n_long[i] = s.charAt(i) - '0'; }
$S$ $\rightarrow$ $L_C$ $(\ \text{String}\ \rightarrow \text{char [ ]}\ )$
char[] c = s.toCharArray();
$S$ $\rightarrow$ $L_S$ $(\ \text{String}\ \rightarrow \text{String [ ]}\ )$
String[] l_s = s.split("");
$L_{N_\text{int}}$ $\rightarrow$ $N_\text{int}$ $(\ \text{int [ ]}\ \rightarrow \text{int}\ )$
String s = ""; for (int i = 0; i < l_n_int.length; i++) { s += l_n_int[i]; } int n_int = Integer.parseInt(s);
$L_{N_\text{long}}$ $\rightarrow$ $N_\text{long}$ $(\ \text{long [ ]}\ \rightarrow \text{long}\ )$
String s = ""; for (int i = 0; i < l_n_long.length; i++) { s += l_n_long[i]; } long n_long = Long.parseLong(s);
$L_{N_\text{int}}$ $\rightarrow$ $S$ $(\ \text{int [ ]}\ \rightarrow \text{String}\ )$
String s = ""; for (int i = 0; i < l_n_int.length; i++) { s += l_n_int[i]; }
$L_{N_\text{long}}$ $\rightarrow$ $S$ $(\ \text{long [ ]}\ \rightarrow \text{String}\ )$
String s = ""; for (int i = 0; i < l_n_long.length; i++) { s += l_n_long[i]; }
$L_{N_\text{int}}$ $\rightarrow$ $L_C$ $(\ \text{int [ ]}\ \rightarrow \text{char [ ]}\ )$
char[] l_c = new char[l_n_int.length]; for (int i = 0; i < l_n_int.length; i++) { l_c[i] = (char) (l_n_int[i]); }
$L_{N_\text{long}}$ $\rightarrow$ $L_C$ $(\ \text{long [ ]}\ \rightarrow \text{char [ ]}\ )$
char[] l_c = new char[l_n_long.length]; for (int i = 0; i < l_n_long.length; i++) { l_c[i] = (char) (l_n_long[i]); }
$L_{N_\text{int}}$ $\rightarrow$ $L_S$ $(\ \text{int [ ]}\ \rightarrow \text{String [ ]}\ )$
String[] l_s = new String[l_n_int.length]; for (int i = 0; i < l_n_int.length; i++) { l_s[i] = String.valueOf(l_n_int[i]); }
$L_{N_\text{long}}$ $\rightarrow$ $L_S$ $(\ \text{long [ ]}\ \rightarrow \text{String [ ]}\ )$
String[] l_s = new String[l_n_long.length]; for (int i = 0; i < l_n_long.length; i++) { l_s[i] = String.valueOf(l_n_long[i]); }
$L_C$ $\rightarrow$ $S$ $(\ \text{char [ ]}\ \rightarrow \text{String}\ )$
String s = new String(l_c);
$L_C$ $\rightarrow$ $L_{N_\text{int}}$ $(\ \text{char [ ]}\ \rightarrow \text{int [ ]}\ )$
int[] l_n_int = new int[l_c.length]; for (int i = 0; i < l_c.length; i++) { l_n_int[i] = l_c[i] - '0'; }
$L_C$ $\rightarrow$ $L_{N_\text{long}}$ $(\ \text{char [ ]}\ \rightarrow \text{long [ ]}\ )$
long[] l_n_long = new long[l_c.length]; for (int i = 0; i < l_c.length; i++) { l_n_long[i] = l_c[i] - '0'; }
$L_C$ $\rightarrow$ $L_S$ $(\ \text{char [ ]}\ \rightarrow \text{String [ ]}\ )$
String[] l_s = new String[l_c.length]; for (int i = 0; i < l_c.length; i++) { l_s[i] = String.valueOf(l_c[i]); }
$L_S$ $\rightarrow$ $N_\text{int}$ $(\ \text{String [ ]}\ \rightarrow \text{int}\ )$
int n_int = Integer.parseInt(String.join("", l_s));
$L_S$ $\rightarrow$ $N_\text{long}$ $(\ \text{String [ ]}\ \rightarrow \text{long}\ )$
long n_long = Long.parseLong(String.join("", l_s));
$L_S$ $\rightarrow$ $S$ $(\ \text{String [ ]}\ \rightarrow \text{String}\ )$
String s = String.join("", l_s);
$L_S$ $\rightarrow$ $L_{N_\text{int}}$ $(\ \text{String [ ]}\ \rightarrow \text{int [ ]}\ )$
int[] l_n_int = new int[l_s.length]; for (int i = 0; i < l_s.length; i++) { l_n_int[i] = Integer.parseInt(l_s[i]); }
$L_S$ $\rightarrow$ $L_{N_\text{long}}$ $(\ \text{String [ ]}\ \rightarrow \text{long [ ]}\ )$
long[] l_n_long = new long[l_s.length]; for (int i = 0; i < l_s.length; i++) { l_n_long[i] = Long.parseLong(l_s[i]); }
$L_S$ $\rightarrow$ $L_C$ $(\ \text{String [ ]}\ \rightarrow \text{char [ ]}\ )$
char[] l_c = new char[l_s.length]; for (int i = 0; i < l_s.length; i++) { l_c[i] = l_s[i].charAt(0); }