Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

21.06.26 - [PG] 다트 게임 #98

Closed
suhyunsim opened this issue Jun 27, 2021 · 0 comments
Closed

21.06.26 - [PG] 다트 게임 #98

suhyunsim opened this issue Jun 27, 2021 · 0 comments
Assignees
Labels
lv.1 프로그래머스 - level 1 문자열 문자열 실패 시도했지만 맞지 못한 문제

Comments

@suhyunsim
Copy link
Owner

suhyunsim commented Jun 27, 2021

문제

핵심 아이디어

어려운 점, 실수

  • 어려운 문제는 아니었는데 예상한 시간 내에 풀지 못했다. -> 다시 연습 겸 풀어보는게 좋을 듯

풀이

package main.java.com.poogle.PG.Q17862;

public class Solution {
    public static int solution(String dartResult) {
        int[] answer = new int[3];
        int idx = 0;
        int cnt = 0;
        for (int i = 0; i < dartResult.length(); i++) {
            char status = dartResult.charAt(i);
            int dart = Character.getNumericValue(status);
            if (dart >= 0 && dart <= 10) {
                if (dart == 1) {
                    if (Character.getNumericValue(dartResult.charAt(i + 1)) == 0) { //숫자가 10일 때
                        dart = 10;
                        i++;
                    }
                }
                answer[idx] = dart;
                cnt++;
            } else {
                switch (status) {
                    case 'S':
                        answer[idx] = (int) Math.pow(answer[idx], 1);
                        idx++;
                        break;
                    case 'D':
                        answer[idx] = (int) Math.pow(answer[idx], 2);
                        idx++;
                        break;
                    case 'T':
                        answer[idx] = (int) Math.pow(answer[idx], 3);
                        idx++;
                        break;
                    case '*':
                        idx = idx - 2 < 0 ? 0 : idx - 2; //맨 앞일 때 0
                        while (idx < cnt) { //가장 최근 2개의 점수에 2 곱하기
                            answer[idx] = answer[idx] * 2;
                            idx++;
                        }
                        break;
                    case '#':
                        answer[idx - 1] = answer[idx - 1] * (-1);
                        break;
                }
            }
        }
        return answer[0] + answer[1] + answer[2];
    }

    public static void main(String[] args) {
        System.out.println(solution("1S2D*3T"));
        System.out.println(solution("1D2S#10S"));
    }
}
@suhyunsim suhyunsim self-assigned this Jun 27, 2021
@suhyunsim suhyunsim added lv.1 프로그래머스 - level 1 문자열 문자열 실패 시도했지만 맞지 못한 문제 labels Jun 28, 2021
@suhyunsim suhyunsim added this to the 6월 4주 차 milestone Jun 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lv.1 프로그래머스 - level 1 문자열 문자열 실패 시도했지만 맞지 못한 문제
Projects
None yet
Development

No branches or pull requests

1 participant