[프로그래머스][Java] [1차] 다트 게임

Boknami·2023년 8월 11일
0

프로그래머스

목록 보기
16/29

🕐 풀이 시간 : 30분

🌄시행착오

(점수)(마크)
같은 문제에서 내가 너무 크게 간과한 부분이있었다.
보통 이런 경우에는 char형으로 뽑아서 그게 숫자 부분이면 숫자로 판단하고 마크와 조합을 했는데..

만약 점수가 10점 이상이라면?
그럼 숫자를 1,0 이렇게 두 번을 발견하고 점수를 갱신해버린다.

💡아이디어

아이디어라보다 문제를 읽고 이해하는데 오래 걸렸다.
String값을 하나씩 보면서 확인해야한다.

실패코드

class Solution {
    public int solution(String dartResult) {
        //S => 얻은 점수의 1제곱
        //D => 얻은 점수의 2제곱
        //T => 얻은 점수의 3제곱
        //* => 바로 전 얻은 점수를 각 2배로.
        //# => 해당 점수 마이너스
        int scoreList[] = {0,0,0};
        int answer = 0;
        int cnt = 0;
        int score = 0;
            
        for(int i = 0; i < dartResult.length() ; i++){
            char v = dartResult.charAt(i);
            int vNum = v - '0';
            System.out.println(scoreList[0] + " " + scoreList[1]  + " " + scoreList[2]);
            if(vNum >= 0 && vNum <= 9){
                score = vNum;
            }
            else if(v == 'D' || v == 'T' || v == 'S'){
                if(v == 'S'){
                    scoreList[cnt] = score;
                    cnt++;
                }
                else if(v == 'D'){
                    scoreList[cnt] = score * score;
                    cnt++;
                }
                else if(v == 'T'){
                    scoreList[cnt] = score * score * score;
                    cnt++;
                }
            }
            else if(v == '*'){
                if(cnt == 3) cnt -= 1;
                for(int j = 0 ; j <= cnt; j++)
                    scoreList[j] = scoreList[j] * 2;
                
            }
            else if(v == '#'){
                if(cnt == 3) cnt -= 1;
                scoreList[cnt] -= score;
            }
        }
        return scoreList[0] + scoreList[1] + scoreList[2];
    }
}

성공코드

class Solution {
    public int solution(String dartResult) {
        int answer = 0;
        int[] score = new int[3];

        int getScore=0;
        int idx=0;
        String mixture="";
        
        for(int i=0;i<dartResult.length();i++){
            char c = dartResult.charAt(i);
            
            if(c>='0'&&c<='9'){       
                 mixture+=String.valueOf(c);
            }
            else if(c=='S'||c=='D'||c=='T'){
               getScore = Integer.parseInt(mixture);
                
               if(c=='S'){
                   score[idx++]= getScore;
                 }
                else if(c=='D'){
                    score[idx++]= getScore*getScore;
                }
                else{
                    score[idx++]=getScore*getScore*getScore;
                } 
                mixture="";
            }
            else {
                if(c=='*'){
                    score[idx-1]*=2;
                    if(idx-2>=0) score[idx-2]*=2; 
                }
                else {
                score[idx-1]*=(-1);
                }
            }
        }
        answer=score[0]+score[1]+score[2];
        return answer;
    }
}

0개의 댓글

관련 채용 정보