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
| import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class Solution {
public int solution(String dartResult) {
int answer = 0;
int tmp = 0;
int preScore = 0; // 이전 점수 저장용 변수
boolean pre_bonus_star = false; // 이전 점수에서 스타상을 받았는지?
Pattern pattern = Pattern.compile("[0-9]+[SDT]\\W?");
Matcher m = pattern.matcher(dartResult);
while(m.find()) {
String dart = m.group();
int score = 0;
if(dart.startsWith("10")) score = 10;
else score = Integer.parseInt(dart.substring(0, 1));
String area = "S";
if(dart.contains("D")) area = "D";
if(dart.contains("T")) area = "T";
String bonus = "";
if(dart.contains("*")) bonus = "*";
if(dart.contains("#")) bonus = "#";
// 계산
if(area.equals("S")) {
tmp = score;
} else if(area.equals("D")) {
tmp = score*score;
} else if (area.equals("T")){
tmp = score*score*score;
}
if(bonus.equals("*")) {
tmp = tmp * 2 + preScore;
preScore = tmp - preScore;
} else if(bonus.equals("#") && pre_bonus_star) {
tmp *= (-2);
preScore = tmp;
} else if(bonus.equals("#")) {
tmp *= (-1);
preScore = tmp;
} else {
preScore = tmp;
}
pre_bonus_star = bonus.equals("*");
answer += tmp;
}
return answer;
}
}
|