笔试处理输入模板

1
2
3
4
5
6
7
8
9
10
11
12
13
14

// 接受一行作为字符数组,再将字符数组中每个字符转换为整型
import java.io.*;
import java.util.*;
public class {

public static void main(String[] args)throws IOException{
BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
String [] a = b.readLine().trim().split(" ");
int n = Integer.parseInt(a[0]);
long w = Integer.parseInt(a[1]);
}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

// 接收第一行一个 long 作为数组大小,然后接收后面一行作为字符数组,再将字符数组转换为整型数组
import java.util.*;
import java.io.*;
public class {

public static void main(String[]args)throws IOException{
BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(b.readLine());
long[] arr = new long[n];
String[] strs = b.readLine().trim().split(" ");

for(int i= 0; i < n;i++){
arr[i] = Integer.parseInt(strs[i]);
}
b.close();
}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// 使用 Scanner
// 先接收一个 int 作为数组大小,随后接受数组中的数
import java.util.Scanner;
import java.util.PriorityQueue;

public class {

public static void main(String[] args) {
// 接收数组 nums ,数组长度 nums_len
Scanner scanner = new Scanner(System.in);
int nums_len = scanner.nextInt();
int[] nums = new int[nums_len];
for (int i = 0; i < nums.length; i++) {
nums[i] = scanner.nextInt();
}
scanner.close();
}

}
1
2
3
4
5
6
7
8
9
10
11
12
// 数组转换为链表
// n 为链表长度,返回链表头节点
private static Node createNode(String[] str,int n){
Node head = new Node(Integer.parseInt(str[0]));
Node node = head;
for(int i=1;i<n;i++){
Node newNode = new Node(Integer.parseInt(str[i]));
node.next = newNode;
node = newNode;
}
return head;
}
1
2
3
4
5
6
7
8
9
//链表节点结构
class Node{
int val;
Node head;
Node next;
Node(int val){
this.val = val;
}
}