티스토리 뷰

PS/programmers

Level1) 다트게임

kingsubin 2021. 4. 27. 10:32
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
/*
    점수|보너스|[옵션]
    점수 : 0 ~ 10
    보너스 : S, D, T
    옵션 : '*', '#', 없을수있음
    * : 이전라운드 * 2, 현재라운드 * 2
    # : 현재라운드 * -1
 */
 
import java.util.ArrayList;
import java.util.List;
 
public class Level1_다트게임 {
    static int[] roundCount = new int[3];
 
    public static int solution(String dartResult) {
        int answer = 0;
 
        // 라운드 인덱스 뽑기
        List<Integer> lastIndexs = new ArrayList<>();
        String[] rounds = new String[3];
 
        for (int i = 0; i < dartResult.length(); i++) {
            char c = dartResult.charAt(i);
            if (c == 'S' || c == 'D' || c == 'T') {
                if ((i+1 < dartResult.length()) && (dartResult.charAt(i+1== '#' || dartResult.charAt(i+1== '*')) {
                    // 옵션 존재
                    lastIndexs.add(i+1);
                } else {
                    // 옵션 없음
                    lastIndexs.add(i);
                }
            }
        }
 
        // 각 라운드 구별
        rounds[0= dartResult.substring(0, lastIndexs.get(0+ 1);
        rounds[1= dartResult.substring(lastIndexs.get(0+ 1, lastIndexs.get(1+ 1);
        rounds[2= dartResult.substring(lastIndexs.get(1+ 1, lastIndexs.get(2+ 1);
 
        // 각 라운드 점수 구하기
        for (int i = 0; i < 3; i++) {
            roundCount[i] = getCount(rounds[i], i);
        }
 
        // 점수 합산
        for (int i : roundCount) {
            answer += i;
        }
        return answer;
    }
 
    private static int getCount(String round, int currentRound) {
        int length = round.length();
        int result = round.charAt(0- '0';
        char bonus = round.charAt(1);
        char option = '0';
        if (length > 2) {
            option = round.charAt(2);
        }
 
        if (round.contains("10")) {
            result = 10;
            bonus = round.charAt(2);
 
            if (length == 4) {
                option = round.charAt(3);
            }
        }
 
        if (bonus == 'D') {
            result = result * result;
        } else if (bonus == 'T') {
            result = result * result * result;
        }
 
        if (option != '0') {
            result = checkOption(option, result, currentRound);
        }
 
        return  result;
    }
 
    private static int checkOption(char option, int result, int currentRound) {
        if (option == '*') {
            result *= 2;
            if (currentRound > 0) {
                roundCount[currentRound-1*= 2;
            }
        } else if (option == '#'){
            result *= -1;
        }
        return result;
    }
 
    public static void main(String[] args) {
        String dartResult = "1D2S#10S";
        int solution = solution(dartResult);
        System.out.println(solution);
    }
}
 
cs

'PS > programmers' 카테고리의 다른 글

Level1) 3진법 뒤집기  (0) 2021.04.28
Level1) 키패드 누르기  (0) 2021.04.27
Level1) 비밀지도  (0) 2021.03.17
Level1) 실패율  (0) 2021.03.16
Level1) 예산  (0) 2021.03.16