问题一

给定一个数组,计算出现次数超过两个的数字的和

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
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class question1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
HashMap<Integer, Integer> map = new HashMap<>();
String s = in.nextLine();
String s1 = s.substring(1, s.length() - 1);
String[] num = s1.split(" ");
for(String q:num){
int tmp = Integer.parseInt(q);
map.put(tmp,map.getOrDefault(tmp,0)+1);
}
int res = 0;
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
//System.out.println(entry.getKey());
if (entry.getValue() >= 2) {
res += entry.getKey();
}
}
System.out.println(res);
}
}

[!NOTE]

这里的数组其实是个字符串,笔者刚开始没仔细看题目直接红温了

问题二

一行输入,给定M,D,T,M表示魔法值,D表示到安全地区的距离,T表示剩余时间(秒)。

每秒钟的选择包括

  • 消耗10魔法值,前进60

  • 不消耗魔法值,前进17

  • 不前进,获得4魔法值

如果能够到达安全地区,打印YES+最短时间

如果不能到达安全地区,打印NO+最大距离

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
import java.util.Scanner;

public class question2 {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int M, D;
int T;
M = in.nextInt();
D = in.nextInt();
T = in.nextInt();
int len = 0;
len = judgeDistance(M, T, 0, D)[0];
int tt = judgeDistance(M, T, 0, D)[1];
if (len >= D) {
System.out.print("YES ");
System.out.println(T-tt);
} else {
System.out.print("NO ");
System.out.println(len);
}

}

static int[] judgeDistance(int M, int T, int dis, int D) {
int[] res = new int[2];
if (T == 0) {
res[0] = dis;
res[1] = T;
return res;
}
if (dis >= D && T >= 0) {
res[0] = dis;
res[1] = T;
return res;
}
int minT = 0;

if (M >= 10) {
int d1 = judgeDistance(M - 10, T - 1, dis + 60, D)[0];
int t1 = judgeDistance(M - 10, T - 1, dis + 60, D)[1];
int d2 = judgeDistance(M + 4, T - 1, dis, D)[0];
int t2 = judgeDistance(M + 4, T - 1, dis, D)[1];
int d3 = judgeDistance(M, T - 1, dis + 17, D)[0];
int t3 = judgeDistance(M, T - 1, dis + 17, D)[1];
minT = Math.max(t1, Math.max(t2, t3));
dis = Math.max(d1, Math.max(d2, d3));

} else {
int d2 = judgeDistance(M + 4, T - 1, dis, D)[0];
int t2 = judgeDistance(M + 4, T - 1, dis, D)[1];
int d3 = judgeDistance(M, T - 1, dis + 17, D)[0];
int t3 = judgeDistance(M, T - 1, dis + 17, D)[1];
minT = Math.max(t2,t3);
dis = Math.max(d2, d3);
}
res[0] = dis;
res[1] = minT;
return res;
}
}

[!NOTE]

采用抽象递归,应该还有更好的办法

问题三

这里简短阐述一下题目意思

命令:

  1. I 命令,加入一个音乐,以及其音乐种类

  2. P 命令,播放音乐,该音乐好感度+3

  3. B 命令,中断音乐,该音乐好感度-2

补充:

  1. 如果播放完的音乐和上一次播放完的音乐类型相同,那么该类型其他歌曲好感度+1

  2. 如果中断的音乐和上一次中断的音乐类型相同,那么该类型其他歌曲好感度-1

  3. UnkownStyle类型只作用于自己的好感度

[!WARNING]

笔者没有AC,通过率85%,仅供参考

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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Scanner;

public class question3 {

public static void main(String[] args) {
HashMap<String, Music> hashMap = new HashMap<>();
Scanner in = new Scanner(System.in);
int id = 0;
Music lastPlayMusic = null;
Music lastBanMusic = null;
while (in.hasNextLine()) {
String commandLine = in.nextLine();
if (commandLine.equals("")) {
break;
}
String[] command = commandLine.split(" ");
if (command[0].equals("I")) {
Music tmp = new Music(command[1], command[2], id);
id++;
hashMap.put(command[1], tmp);
}
else if (command[0].equals("B")) {
Music tmp = hashMap.get(command[1]);
tmp.love -= 2;
hashMap.put(command[1], tmp);
String type = tmp.type;
int tmpID = tmp.id;
if (type.equals("UnkownStyle")) {
lastBanMusic = tmp;
continue;
}
if(lastBanMusic == null){
lastBanMusic = tmp;
continue;
}
if (lastBanMusic.type.equals(type)) {
ArrayList<String> sameList = new ArrayList<>();
for (Map.Entry<String, Music> entry : hashMap.entrySet()) {
Music amusic = entry.getValue();
if (amusic.type.equals(type) && amusic.id != tmpID) {
sameList.add(amusic.name);
}
}
for (String a : sameList) {
Music tmpMusic = hashMap.get(a);
tmpMusic.love--;
hashMap.put(a,tmpMusic);
}
}
lastBanMusic = tmp;
}
else if (command[0].equals("P")) {
Music tmp = hashMap.get(command[1]);
tmp.love += 3;
hashMap.put(command[1], tmp);
String type = tmp.type;
int tmpID = tmp.id;
if (type.equals("UnkownStyle")) {
lastPlayMusic = tmp;
continue;
}
if(lastPlayMusic == null){
lastPlayMusic = tmp;
continue;
}
if (lastPlayMusic.type.equals(type)) {
ArrayList<String> sameList = new ArrayList<>();
for (Map.Entry<String, Music> entry : hashMap.entrySet()) {
Music amusic = entry.getValue();
if (amusic.type.equals(type) && amusic.id != tmpID) {
sameList.add(amusic.name);
}
}
for (String a : sameList) {
Music tmpMusic = hashMap.get(a);
tmpMusic.love++;
//System.out.println(tmpMusic.name+" love increased 1");
hashMap.put(a,tmpMusic);
}
}
lastPlayMusic = tmp;
}
}
ArrayList<Music> arrayList = new ArrayList<>();
for(Map.Entry<String,Music> entry:hashMap.entrySet()){
arrayList.add(entry.getValue());
}
arrayList.sort(new Comparator<Music>() {
@Override
public int compare(Music o1, Music o2) {
if(o1.love!=o2.love){
return o2.love-o1.love;
}else{
return o1.name.compareTo(o2.name);
}

}
});

for(Music music:arrayList){
System.out.println(music.name+" "+music.love);
}

}
}

class Music {
public String name;
public String type;
public int love;

public int id;

public Music(String name1, String type1, int id1) {
name = name1;
type = type1;
id = id1;
love = 0;
}
}