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));
}
}
'ALGORITHM > 프로그래머스 | 백준 | 삼성 | 카카오' 카테고리의 다른 글
[프로그래머스] 2018 KAKAO BLIND RECRUITMENT 캐시 (0) | 2020.11.09 |
---|---|
[백준] 14889번 스타트와 링크 (0) | 2020.10.25 |
[SWEA] 5658. [모의 SW 역량테스트] 보물상자 비밀번호 (0) | 2020.10.20 |
[백준] 14891번 톱니바퀴 (0) | 2020.09.13 |
[백준] 17144번 미세먼지 안녕! (0) | 2020.08.28 |