Java算法题常见的20种输入模板(ACM / LeetCode 通用)

在刷算法题时,很多人一开始都会被 输入输出格式 卡住。实际上,大部分平台(如 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());

    }
}

四、总结

算法题输入格式其实 高度固定,核心就几类:

  1. 单个整数

  2. 一行多个整数

  3. 数组

  4. 矩阵

  5. 多组测试

  6. 图结构

  7. 字符串

  8. EOF输入

只要掌握这些模板,基本 不会再被输入格式卡住


如果你在刷算法题,建议再掌握:

  • Java算法竞赛万能模板

  • BFS / DFS模板

  • 并查集模板

  • 最短路模板

  • 动态规划模板

这些是算法题中最常出现的核心结构。

相关推荐
摇滚侠14 小时前
MyBatis 入门到项目实战 MyBatis 的缓存 56-61
java·缓存·mybatis
让我上个超影吧14 小时前
Claude code:Hooks
java·数据库·ai编程
RH23121114 小时前
2026.6.8Linux
java·数据库·中间件
于指尖飞舞15 小时前
java后端面试题(多线程极简)
java·开发语言
IT 行者15 小时前
GitHub Spec Kit 实战(四):读懂和干预 /speckit.plan——AI 最自由发挥的一步
java·人工智能·github·ai编程·claude
独隅15 小时前
IntelliJ IDEA 在 Windows 上的完整安装与使用指南
java·windows·intellij-idea
Misnearch15 小时前
为什么List<int[]> ans = new ArrayList<>()能成功创建
java·object
梦想的颜色15 小时前
从零入门:Docker在Ubuntu上的安装、使用与主流镜像仓库实战(Java/Go/MySQL/PostgreSQL/MongoDB/Nginx
java·ubuntu·docker
是多巴胺不是尼古丁15 小时前
期末java复习--string
java·开发语言·python
Survivor00115 小时前
高并发系统流量治理的底层算法
java·开发语言