preface

Take the niuke paper as an example, the example of input processing in ACM mode

ZJ1 Additional questions (multiple lines multiple ints)

ZJ1 Additional topic address

The first line has an int number, the second line has multiple int numbers,

So let’s store a number in the first row, and ArrayList in the second row.

import java.util.Scanner; import java.util.List; import java.util.ArrayList; public class Main { public static List<Integer> list = new ArrayList<>(); public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); while (in.hasNextInt()) { list.add(in.nextInt()); } System.out.println(n); System.out.println(list); }}Copy the code

ZJ2 programming Problem 1 (Int and int Matrix)

ZJ2 additional topic address

Similar to the first question, this time we store the matrix with a List<List<\Integer>> :

import java.util.Scanner; import java.util.ArrayList; import java.util.List; public class Main { public static List<List<Integer>> matrix = new ArrayList<>(); public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); while (in.hasNextInt()) { List<Integer> row = new ArrayList<>(); row.add(in.nextInt()); row.add(in.nextInt()); row.add(in.nextInt()); row.add(in.nextInt()); matrix.add(new ArrayList(row)); } System.out.println(t); System.out.println(matrix); }}Copy the code

ZJ3 programming problem 2 (Two ints and one String)

ZJ3 Additional topic address

A full String is scanned out using the next() function

import java.util.Scanner; public class Main { public static String s; public static void main(String[] args) { Scanner in = new Scanner(System.in); int a = in.nextInt(); int b = in.nextInt(); while (in.hasNext()) { s = in.next(); } System.out.println(a + " " + b); System.out.println(s); }}Copy the code

ZJ4 Additional questions (one line int)

ZJ4 Additional topic address

You can just put it in a list:

import java.util.Scanner; import java.util.List; import java.util.ArrayList; public class Main { public static List<Integer> list = new ArrayList<>(); public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextInt()) { list.add(in.nextInt()); } System.out.println(list); }}Copy the code

ZJ5 programming problem 1 (multiple ints multiple Strings (character matrix))

ZJ5 Additional topic address

Note that the input character matrix is given as a multi-line String, and that next() takes the String and then charAt() takes the char.

To construct the character matrix, I use List<List<>> for printing purposes, or I can use char[][] :

import java.util.Scanner; import java.util.List; import java.util.ArrayList; public class Main { public static List<List<Character>> chars; public static void main(String[] args) { Scanner in = new Scanner(System.in); int row = in.nextInt(); int col = in.nextInt(); chars = new ArrayList<>(); while (in.hasNext()) { for (int i = 0; i < row; i++) { List<Character> temp = new ArrayList<>(); String rowString = in.next(); for (int j = 0; j < col; j++) { temp.add(rowString.charAt(j)); } chars.add(temp); } } System.out.println(chars); }}Copy the code

Char [] [] form:

import java.util.Scanner; import java.util.List; import java.util.ArrayList; public class Main { public static char[][] chars; public static void main(String[] args) { Scanner in = new Scanner(System.in); int row = in.nextInt(); int col = in.nextInt(); chars = new char[row][col]; while (in.hasNext()) { for (int i = 0; i < row; i++) { char[] rowString = in.next().toCharArray(); chars[i] = rowString; } } printFunc(chars); } public static void printFunc(char[][] chars) { for (int i = 0; i < chars.length; i++) { for (int j = 0; j < chars[0].length; j++) { System.out.print(chars[i][j]); } System.out.println(); }}}Copy the code

MGJ8 linked list merge

MGJ8 Additional topic address

There are two ways to do it, one is to really do it as A list, and the other is to do it as an array.

Follow the linked list:

import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); ListNode list1 = createList(in.nextLine().split(" ")); ListNode list2 = createList(in.nextLine().split(" ")); while (list1 ! = null) { System.out.print(list1.val + " "); list1 = list1.next; } System.out.println(); while (list2 ! = null) { System.out.print(list2.val + " "); list2 = list2.next; } } public static ListNode createList(String[] str){ if (str == null || str.length == 0) return null; ListNode pre = new ListNode(0); ListNode head = pre; for (int i = 0; i < str.length; i++) { head.next = new ListNode(Integer.parseInt(str[i])); head = head.next; } return pre.next; } } 1 class ListNode { int val; ListNode next; public ListNode(int val) { this.val = val; }}Copy the code

Int [] form:

import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String[] s1 = in.nextLine().split(" "); String[] s2 = in.nextLine().split(" "); int[] list1 = new int[s1.length]; int[] list2 = new int[s2.length]; for (int i = 0; i < s1.length; i++) { list1[i] = Integer.parseInt(s1[i]); System.out.print(list1[i] + " "); } System.out.println(); for (int j = 0; j < s2.length; j++) { list2[j] = Integer.parseInt(s2[j]); System.out.print(list2[j] + " "); }}}Copy the code