前言
以牛客笔试题为例,ACM模式下的输入处理示例
ZJ1 附加题 (多行多个int)
ZJ1 附加题题目地址
第一行一个int数字,第二行有多个int数字,
那我们第一个存一个数字,第二行用ArrayList存起来。
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);
}
}
ZJ2 编程题1 (int和int矩阵)
ZJ2 附加题题目地址
和第一个题目类似,矩阵这次我们用一个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);
}
}
ZJ3 编程题2 (两个int和一个String)
ZJ3 附加题题目地址
一个完整的String用next()函数扫描出来
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);
}
}
ZJ4 附加题 (一行int)
ZJ4 附加题题目地址
可以直接放一个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);
}
}
ZJ5 编程题1 (多个int多个String(字符矩阵))
ZJ5 附加题题目地址
值得注意的是输入的字符矩阵是用多行String来给的,next()拿到String之后还要用charAt()来取其中的char。
这里要构建字符矩阵,我为了方便打印使用了List<List<>>来存,当然也可以用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);
}
}
char[][] 形式:
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();
}
}
}
MGJ8 链表合并
MGJ8 附加题题目地址
有两种处理方法,一种是真的老老实实按照链表处理,一种是按照数组处理(题目能A就行,怎么简单怎么来)
按照链表处理:
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;
}
}
int[] 形式:
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] + " ");
}
}
}
|