在刷算法题时,很多人一开始都会被 输入输出格式 卡住。实际上,大部分平台(如 LeetCode、Codeforces、AtCoder、洛谷、ACM竞赛)的输入格式都非常固定。
本文整理了 20种最常见的Java输入模板 ,基本覆盖算法题 90%+ 的输入格式,可以直接复制使用。
一、算法题常用输入方式
Java常见有两种输入方式:
1. Scanner(简单但慢)
Scanner sc = new Scanner(System.in);
优点:
-
写法简单
-
适合初学者
缺点:
- 数据量大时会 超时
2. BufferedReader(竞赛推荐)
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
优点:
-
读取速度快
-
适合ACM / 大数据输入
下面进入 20种经典输入模板。
二、20种算法题输入模板(Java)
1 输入一个整数
输入
5
代码
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
2 输入两个整数
输入
3 7
代码
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
3 一行多个整数
输入
1 2 3 4 5
代码
Scanner sc = new Scanner(System.in);
int[] arr = new int[5];
for(int i = 0; i < 5; i++){
arr[i] = sc.nextInt();
}
4 n + 数组
输入
5
1 2 3 4 5
代码
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for(int i = 0; i < n; i++){
arr[i] = sc.nextInt();
}
5 n行数组
输入
5
1
2
3
4
5
代码
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for(int i = 0; i < n; i++){
arr[i] = sc.nextInt();
}
6 二维矩阵
输入
3 3
1 2 3
4 5 6
7 8 9
代码
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int[][] matrix = new int[n][m];
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
matrix[i][j] = sc.nextInt();
}
}
7 字符矩阵
输入
3 3
abc
def
ghi
代码
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
char[][] grid = new char[n][m];
for(int i = 0; i < n; i++){
String s = sc.next();
grid[i] = s.toCharArray();
}
8 输入字符串
输入
hello
代码
Scanner sc = new Scanner(System.in);
String s = sc.next();
9 输入整行字符串
输入
hello world
代码
Scanner sc = new Scanner(System.in);
String line = sc.nextLine();
注意:
如果前面用了 nextInt(),需要先执行一次
sc.nextLine();
10 多组测试数据
输入
3
5
10
20
代码
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for(int i = 0; i < t; i++){
int n = sc.nextInt();
}
11 读取到EOF(ACM经典)
输入
1 2
3 4
5 6
代码
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int a = sc.nextInt();
int b = sc.nextInt();
}
12 图输入(边)
输入
5 4
1 2
1 3
2 4
3 5
代码
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
for(int i = 0; i < m; i++){
int u = sc.nextInt();
int v = sc.nextInt();
}
13 带权图
输入
4 4
1 2 5
1 3 2
2 4 3
3 4 7
代码
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
for(int i = 0; i < m; i++){
int u = sc.nextInt();
int v = sc.nextInt();
int w = sc.nextInt();
}
14 邻接表图
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
List<Integer>[] graph = new ArrayList[n + 1];
for(int i = 1; i <= n; i++){
graph[i] = new ArrayList<>();
}
for(int i = 0; i < m; i++){
int u = sc.nextInt();
int v = sc.nextInt();
graph[u].add(v);
graph[v].add(u);
}
15 输入 double
输入
3.14
代码
Scanner sc = new Scanner(System.in);
double x = sc.nextDouble();
16 输入 long
输入
10000000000
代码
Scanner sc = new Scanner(System.in);
long x = sc.nextLong();
17 字符串数组
输入
3
apple
banana
cat
代码
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String[] arr = new String[n];
for(int i = 0; i < n; i++){
arr[i] = sc.next();
}
18 二进制字符串
输入
1010101
代码
Scanner sc = new Scanner(System.in);
String s = sc.next();
19 逗号分隔输入
输入
1,2,3,4
代码
Scanner sc = new Scanner(System.in);
String line = sc.next();
String[] parts = line.split(",");
int[] arr = new int[parts.length];
for(int i = 0; i < parts.length; i++){
arr[i] = Integer.parseInt(parts[i]);
}
20 高性能输入(竞赛推荐)
import java.io.*;
import java.util.*;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
读取数组
st = new StringTokenizer(br.readLine());
int[] arr = new int[n];
for(int i = 0; i < n; i++){
arr[i] = Integer.parseInt(st.nextToken());
}
三、推荐的Java算法竞赛模板
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
}
}
四、总结
算法题输入格式其实 高度固定,核心就几类:
-
单个整数
-
一行多个整数
-
数组
-
矩阵
-
多组测试
-
图结构
-
字符串
-
EOF输入
只要掌握这些模板,基本 不会再被输入格式卡住。
如果你在刷算法题,建议再掌握:
-
Java算法竞赛万能模板
-
BFS / DFS模板
-
并查集模板
-
最短路模板
-
动态规划模板
这些是算法题中最常出现的核心结构。