ALGORITHM/프로그래머스 | 백준 | 삼성 | 카카오

[프로그래머스] 2018 KAKAO BLIND RECRUITMENT

SZCODE 2020. 10. 23. 11:21

programmers.co.kr/learn/courses/30/lessons/17682

 

코딩테스트 연습 - [1차] 다트 게임

 

programmers.co.kr

class Solution {
	public static int solution(String dartResult) {
        int answer = 0, idx = 0;
        int[] arr = new int[3];
        String temp = "";
        
        for (int i = 0; i < dartResult.length(); i++) {
			char c = dartResult.charAt(i);
			
			if(c >= '0' && c<='9')  temp += String.valueOf(c);
			else if(c =='S'|| c=='D' || c=='T') {
				int p = Integer.parseInt(temp);
				
				
				if(c=='S') p = (int) Math.pow(p, 1);
				else if(c=='D') p = (int) Math.pow(p, 2);
				else if(c=='T') p = (int) Math.pow(p, 3);
				
				arr[idx++] = p;
				temp ="";

			}else {
				if(c=='#') arr[idx-1] *= -1;
				else {
					arr[idx-1] *= 2;
					if(idx-2 >= 0) arr[idx-2] *=2;
				}
			}
        }
        for (int i = 0; i < arr.length; i++) {
			answer += arr[i];
		}
        
        return answer;
    }

	public static void main(String[] args) {
		String dartResult = "1S*2T*3S";
		System.out.println(solution(dartResult));
	}

}