面试题随笔-21/4/19

面试总结

输入Scanner的一些总结

  1. 需要无限循环读取的情况

这种无需设置终止条件,while (in.hasNext())即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Scanner in = new Scanner(System.in);

while (in.hasNext()) {
int a = in.nextInt();
int b = in.nextInt();
System.out.println("输出:" + (a + b));
}
示例输入1
1 2
输出:3
结论:
空格间隔,正确

示例输入2
1
2
输出:3
结论:
回车换行,正确
  1. 需要读取n行的情况,每一行又是一个数组的情况下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class Main {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
for (int i = 0; i < n; i++) {
// 用什么数据结构保存,因题目而定,例如二维数组、二叉树等
int[] s = string2int(in.nextLine().split(" "));
// 对每一行的业务逻辑
}
}

// string数组转int数组
private static int[] string2int(String[] s1) {
int[] ints = new int[s1.length];
for (int i = 0; i < s1.length; i++) {
ints[i] = Integer.parseInt(s1[i]);
}
return ints;
}
}
  1. 不知道输入多少行

这种情况,当没有输入直接回车时,则停止输入。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);

String line = null;
while(true) {
line = in.nextLine();
// 没有输入直接回车,则为空字符串
if (line.equals("")) {
break;
}
// 业务逻辑
}
}
}

美团笔试题总结

排序问题,一个集合,有id和数值num两个数字,按照num从大到小排序,当num相同时,则id小的在前;query代表查询,append代表添加。

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
public class Main4 {

/**
* 排序问题,从大到小,然后相同就id小的在前
* 输入:
* 9
* query
* append 1 10
* query
* append 2 20
* query
* append 3 15
* query
* append 1 10
* query
* 输出:
* null
* 1
* 2 1
* 2 3 1
* 1 2 3
*/

public static Map<Integer, Integer> map = new ConcurrentHashMap<>();

public static List<String> result = new ArrayList<String>();

public static void main(String[] args) {
Scanner in = new Scanner(System.in);

int _n;
_n = Integer.valueOf(in.nextLine());

String line = null;
int x = 0, y = 0;
for (int i = 0; i < _n; i++) {
line = in.nextLine();
String[] command = line.split(" ");

if (command[0].equals("query")) {
query();
} else if (command[0].equals("append")) {
x = Integer.valueOf(command[1]);
y = Integer.valueOf(command[2]);
append(x, y);
}
}

for (String result_tmp :
result) {
System.out.println(result_tmp);
}
}

private static void append(int x, int y) {

if (map.containsKey(x)) {
map.put(x, map.get(x) + y);
} else {
map.put(x, y);
}
}

private static void query() {
if (map.size() == 0) {
result.add("null");
} else {
List<Map.Entry<Integer, Integer>> list = new ArrayList<Map.Entry<Integer, Integer>>(map.entrySet());
Collections.sort(list, new Comparator<Map.Entry<Integer, Integer>>() {
@Override
public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2) {

return o2.getValue().compareTo(o1.getValue());
}
});
String s = "";

for (Map.Entry<Integer, Integer> entry :
list) {
s += entry.getKey() + " ";
}

if (!s.equals("")) {
result.add(s);
}
}
}
}

兵力和问题

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import java.util.*;

public class Main5 {

/**
* 兵力和 输入 3 1 => 代表3个1级兵
*
* 输入3 4 3,A有3种兵,B有4种兵,出战的兵的力量必须>=3 所以A=3*4 = 12 ; B=1*3 + 2*5 = 13
* B获胜
* 输入:
* 3 4 3
* 3 1
* 3 4
* 4 2
* 1 3
* 2 5
* 4 2
* 2 1
*
* 输出:
* 12 13
* B
* @param args
*/

public static void main(String[] args) {
Scanner in = new Scanner(System.in);

String nmk;
nmk = in.nextLine();

String[] s = nmk.split(" ");

int n = Integer.valueOf(s[0]);
int m = Integer.valueOf(s[1]);
int k = Integer.valueOf(s[2]);

int[][] a_ints = new int[n][2];
int[][] b_ints = new int[m][2];

String line = null;
String[] s1 = null;

int a_sum = 0, b_sum = 0;
// 输入n行 A的兵力情况
for (int i = 0; i < n; i++) {
line = in.nextLine();
s1 = line.split(" ");
a_ints[i][0] = Integer.parseInt(s1[0]);
int fight = Integer.parseInt(s1[1]);
a_ints[i][1] = fight;
// 是否有资格出战,累加得出A的兵力和
if (fight >= k) {
a_sum += fight * a_ints[i][0];
}
}

// 输入m行 B的兵力情况
for (int i = 0; i < m; i++) {
line = in.nextLine();
s1 = line.split(" ");
b_ints[i][0] = Integer.parseInt(s1[0]);
int fight = Integer.parseInt(s1[1]);
b_ints[i][1] = fight;
// 是否有资格出战,累加得出B的兵力和
if (fight >= k) {
b_sum += fight * b_ints[i][0];
}
}

System.out.println(a_sum + " " + b_sum);
if (a_sum > b_sum) {
System.out.print("A");
} else {
System.out.print("B");
}
}
}

车厢调度问题

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
public class Main6 {

/**
* 车厢调度问题,1 代表追加车厢, 2 代表对两个车厢对调 例如 1 1 A => 将 1车厢 追加到 A列车后 2 A B => 将A,B车厢对调
* 输入:
* 7
* 1 1 A
* 1 2 B
* 1 5 A
* 1 3 C
* 1 4 D
* 2 A B
* 2 B C
*
* 输出:
* 3 1 5 2 4
*/


// 题目给定,列车<=5 挂载车厢数量<=1000,定义一个二维数组即可
public static int[][] result = new int[5][1000];

public static void main(String[] args) {
Scanner in = new Scanner(System.in);

int n;
n = Integer.parseInt(in.nextLine());

// 用一个map存储每个列车,对应二维数组的下标
HashMap<String, Integer> indexHashMap = new HashMap<>();
indexHashMap.put("A", 0);
indexHashMap.put("B", 0);
indexHashMap.put("C", 0);
indexHashMap.put("D", 0);

// 用一个map存储每个列车,与相应的二维数组下标对应起来,初始为0,1,2,3
HashMap<String, Integer> intmap = new HashMap<>();
intmap.put("A", 0);
intmap.put("B", 1);
intmap.put("C", 2);
intmap.put("D", 3);


String line = null;
String[] s1 = null;
int index = 0;
for (int i = 0; i < n; i++) {
line = in.nextLine();
s1 = line.split(" ");
if (s1[0].equals("1")) {
index = indexHashMap.get(s1[2]);
result[intmap.get(s1[2])][index] = Integer.parseInt(s1[1]);
// 下标+1
index++;
indexHashMap.put(s1[2], index);
} else {
int x = intmap.get(s1[1]);
int y = intmap.get(s1[2]);

// 车厢对调
swap_xy(x, y);

// 将map中下标也对应替换
intmap.put(s1[1], y);
intmap.put(s1[2], x);
}

}

for (int i = 0; i < 5; i++) {
for (int k = 0; k < 1000; k++) {
if (result[i][k] != 0) {
System.out.print(result[i][k] + " ");
}
}
}
}

// 车厢对调
private static void swap_xy(int x, int y) {
int[] tmp = result[x];
result[x] = result[y];
result[y] = tmp;
}
}