Skip to content

Commit 56b61bf

Browse files
authored
Merge pull request #256 from zzzyoonnn/main
[Failed] PRG_[1차] 다트 게임(복습 필요)
2 parents 3a04062 + 32f206e commit 56b61bf

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package Week6.공통.다트게임_1차;
2+
import java.util.*;
3+
4+
public class jiyoon {
5+
class Solution {
6+
public int solution(String dartResult) {
7+
int answer = 0;
8+
9+
// 제곱 정보를 저장할 HashMap 생성
10+
HashMap<Character, Integer> map = new HashMap<>();
11+
map.put('S', 1);
12+
map.put('D', 2);
13+
map.put('T', 3);
14+
15+
String number = ""; // 숫자 정보를 저장할 number
16+
ArrayList<Integer> list = new ArrayList<>(); // 점수 정보를 저장할 list
17+
for (int i = 0; i < dartResult.length(); i++) {
18+
char c = dartResult.charAt(i);
19+
20+
if (Character.isDigit(c)) { // 숫자라면
21+
number += c;
22+
} else if (c == 'S' || c == 'D' || c == 'T') { // S, D, T라면
23+
int num = Integer.parseInt(number); // 숫자 정보를 저장했던 문자열을 int형으로 변경
24+
list.add((int)Math.pow(num, map.get(c))); // 점수를 list에 저장
25+
number = ""; // 문자열 초기화
26+
} else if (c == '*') {
27+
list.set(list.size() - 1, list.get(list.size() - 1) * 2); // 해당 점수 2배
28+
29+
if (list.size() != 1) { // 이전 점수 2배
30+
list.set(list.size() - 2, list.get(list.size() - 2) * 2);
31+
}
32+
} else if (c == '#') {
33+
list.set(list.size() - 1, list.get(list.size() - 1) * -1); // 해당 점수 마이너스
34+
}
35+
}
36+
37+
for (int i = 0; i < list.size(); i++) {
38+
answer += list.get(i);
39+
}
40+
return answer;
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)