ACM模式输入输出总结2【大厂笔试/OD机考】(Java语言版本)

1.输入字符串

 此类输入一般只包含单个字符串,如: abc123

 Scanner sc = new Scanner(System.in);
 String s = sc.nextLine(); // 读取一行字符串

2.输入数字

此类输入一般只包含单个数字,如:5

 Scanner sc = new Scanner(System.in);
 int num = sc.nextInt(); // 读取一个整数

3.输入字符串数组

此类输入一般会先在第一行输入长度n,再在第二行输入长度为n的数组。

数组可能会用空格隔开,如:

3
aaa bbb ccc

Scanner sc = new Scanner(System.in);
String[] lst = sc.nextLine().split(" "); // 用空格分割字符串

数组可能会用逗号隔开,如:

3
aaa,bbb,ccc

Scanner sc = new Scanner(System.in);
String[] lst = sc.nextLine().split(","); // 用逗号分割字符串

4.输入数字型数组

此类输入一般会先在第一行输入长度n,再在第二行输入长度为n的数组。

数组可能会用空格隔开,如

3
0 1 2

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();
}

或者(可以处理数组长度n未知的输入)

Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
List<Integer> nums = new ArrayList<>();
while (scanner.hasNextInt()) {
    nums.add(scanner.nextInt());
}

数组可能会用逗号隔开,如:

3
0,1,2

Scanner scanner = new Scanner(System.in);

int n = scanner.nextInt(); // Input array length

List<Integer> lst = new ArrayList<>();
scanner.nextLine(); // Move to the next line after reading the integer

String[] input = scanner.nextLine().split(",");
for (String num : input) {
    lst.add(Integer.parseInt(num.trim()));
}

5.输入二维数组

此类输入一般会先在第一行输入二维数组的行数n和列数吗m,再在接下来的n行输入长度为m的字符串或者数组。

字符串类型 输入:

5 4
aaaa
bbbb
bbcc
ddee
ffgg

Scanner sc = new Scanner(System.in);
int height =sc.nextInt();
int width = sc.nextInt();
char map[][] =new char [height][width];
for(int i=0;i<heiht;i++){
    map[i] =sc.nextLine().toCharArray();    
}

5 4
0 0 0 0 
0 0 0 1 
1 1 1 2
2 2 2 3 
3 3 4 4 

Scanner sc = new Scanner(System.in);
int height = sc.nextInt();
int width = sc.nextInt();
int map[][] = new int [height][width];
for(int i=0;i < heiht;i++){
    for(int j=0;i < width;j++){
        map[i][j] = sc.nextInt();
    }
}

6.输入树或图结构

这种输入一般会告诉你树/图结构的边数,然后输入节点编号之间的连接关系。一般而言,题目会告知输入每一条边时,哪个节点是父/子节点。如:

# 有向图一共有2条边
# 每条边输入时
# 第一个元素是父节点,第二个元素是子节点

2
1 2
1 3

# 一棵形如
#    1
#   / \
#  2   3
# 的树形结构

一般采用构建邻接表的形式来表示一个图,而邻接表可以用哈希表很方便地表示。

Scanner scanner = new Scanner(System.in);

int n = scanner.nextInt(); // Number of nodes
int m = scanner.nextInt(); // Number of edges

List<List<Integer>> g = new ArrayList<>(n + 1);
for (int i = 0; i <= n; i++) {
    g.add(new ArrayList<>());
}

for (int i = 1; i <= m; i++) {
    int u = scanner.nextInt();
    int v = scanner.nextInt();
    // Directed graph
    g.get(u).add(v);

7.输入次数若干,以某个标识符作为停止输入的标志

这种输入一般存在多组输入的情况,以某一标识符作为停止输入的标志。如输入若干行,以END作为终止输入的标志:

0
1
2
END

这种输入一般需要结合while循环进行。

Scanner scanner = new Scanner(System.in);

while (scanner.hasNext()) {
    String str = scanner.next();
    if (str.equals("END")) {
        break;
    }
    // do something
}

8.输出 

一般来说直接print()即可。不可以在主函数中用return代替print()

System.out.println(ans);

 9.ACM模式与核心代码模式异同点总结

 

  • 7
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值