完美世界2020Java开发

完美世界2020Java游戏开发笔试

做了一下完美的Java笔试。发现关于类的东西忘光了,好久不写。编程题的一个题大概是自己创建一个栈,实现一些功能,返回最大最小元素,min,max,以及pop和push四个功能。
确实很简单,但是基础的东西有点忘了,第一题没写出来就直接退了。
虽然事后看到有人直接在Main函数里建一个自带的Stack就可以了。不过僵硬在创建class上了。
发现主要原因好像是忘了在Stack类中加入static修饰,没用本地IDE,半天没有运行成功。
下面重新把两道题都写一下。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import java.util.Scanner;
public class Main {
static class Stack {
private int[] arr;//using array to implement stack
private int num = 0;

public Stack(int n) {
arr = new int[n];
}

public void push(int data) {
arr[num] = data;
num++;
}

public int pop() {
num--;
return arr[num];

}

public int min() {
if (num == 0) return 0;
int ret = arr[num - 1];
for (int i = 0; i < num; i++) {
if (ret > arr[i]) {
ret = arr[i];
}
}
return ret;
}

public int max() {
if (num == 0) return 0;
int ret = arr[num - 1];
for (int i = 0; i < num; i++) {
if (ret < arr[i]) {
ret = arr[i];
}
}
return ret;
}
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(); //given the number
Stack s = new Stack(n);
for (int i = 0; i < n; i++) {
s.push(sc.nextInt()); // the data
}
s.pop();
System.out.println(s.min());
System.out.println(s.max());
}
}

第二题Dijkstra,也重新实现了一下。输入一个数字n,表示n行。
输入第二个数字,表示原点。
剩余每行输入数据如下。
0 1 12 -1 -1 -1
-1 0 9 3 -1 -1
-1 -1 0 -1 5 -1
-1 -1 4 0 13 15
-1 -1 -1 -1 0 4
-1 -1 -1 -1 -1 0
输出的是原点到其他点的最短路径。
1
8
4
13
17

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

public class test {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(); //given the number
int start = sc.nextInt(); //given the start node
int[][] matrix = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
matrix[i][j] = sc.nextInt();
}
}
int[] res = dijkstra(matrix, start);
for (int i = 0; i < res.length; i++) {
if (i != start) {
System.out.println(res[i]);
}
}
}

public static int[] dijkstra(int[][] matrix, int start) {
int[] res = new int[matrix.length];//distence from start to other node
int[] visited = new int[matrix.length];

for (int i = 0; i < matrix.length; i++) {
res[i] = matrix[start][i];
}
res[start] = 0; // node to itself
visited[start] = 1; //visit the start node
for (int step = 1; step <= matrix.length; step++) {
int min = Integer.MAX_VALUE;
int pos = 0;
for (int i = 0; i < matrix.length; i++) {
if (visited[i] == 0 && res[i] != 0 && res[i] != -1 && res[i] < min) {
min = res[i]; //from the start node to other node
pos = i; // record the current closest node from start node
}
}
visited[pos] = 1;
for (int i = 0; i < matrix.length; i++) {
if (matrix[pos][i] != -1 && matrix[pos][i] != 0) {
if (res[i] > res[pos] + matrix[pos][i] || res[i] == -1) { //start node to i > start node->pos->i
res[i] = res[pos] + matrix[pos][i];
}
}
}
}
return res;
}
}
Jie wechat
学就完事