programmers.co.kr/learn/courses/30/lessons/17682
코딩테스트 연습 - [1차] 다트 게임
programmers.co.kr
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class Solution {
public int solution(String dartResult) {
double[] scores = new double[3];
Pattern stagePattern = Pattern.compile("\\d+[SDT][*#]?");
Matcher stageMatcher = stagePattern.matcher(dartResult);
int scoreIndex = 0;
while (stageMatcher.find()){
String currentStage = stageMatcher.group();
String score = null;
String bonus = null;
String option = null;
Pattern scorePattern = Pattern.compile("\\d+");
Matcher scoreMatcher = scorePattern.matcher(currentStage);
if (scoreMatcher.find()){
score = scoreMatcher.group();
}
Pattern bonusPattern = Pattern.compile("[SDT]");
Matcher bonusMatcher = bonusPattern.matcher(currentStage);
if (bonusMatcher.find()){
bonus = bonusMatcher.group();
}
Pattern optionPattern = Pattern.compile("[*#]");
Matcher optionMatcher = optionPattern.matcher(currentStage);
if (optionMatcher.find()){
option = optionMatcher.group();
}
switch (bonus){
case "S":
scores[scoreIndex] = Math.pow(Double.parseDouble(score), 1.0);
break;
case "D":
scores[scoreIndex] = Math.pow(Double.parseDouble(score), 2.0);
break;
case "T":
scores[scoreIndex] = Math.pow(Double.parseDouble(score), 3.0);
break;
}
if (option != null){
switch (option){
case "*":
if (0 < scoreIndex){
scores[scoreIndex - 1] *= 2.0;
}
scores[scoreIndex] *= 2.0;
break;
case "#":
scores[scoreIndex] *= -1.0;
}
}
scoreIndex++;
}
return (int) Arrays.stream(scores).sum();
}
}
'Problem-solving > 프로그래머스' 카테고리의 다른 글
프로그래머스 - (2020 KAKAO BLIND RECRUITMENT) 괄호 변환 (Java) (0) | 2020.11.23 |
---|---|
프로그래머스 - (2020 KAKAO BLIND RECRUITMENT) 문자열 압축 (Java) (0) | 2020.11.23 |
프로그래머스 - (2019 KAKAO BLIND RECRUITMENT) 실패율 (Java) (0) | 2020.11.22 |
프로그래머스 - (2018 KAKAO BLIND RECRUITMENT) [1차]비밀지도 (Java) (0) | 2020.11.21 |
프로그래머스 - (2020 카카오 인턴십) 키패드 누르기 (Java) (0) | 2020.11.21 |
댓글