[ 2018 Kakao BLIND RECRUITMENT - 다트 게임 1차 ]
https://programmers.co.kr/learn/courses/30/lessons/17682
문제 아래처럼 1 ~ 9 단계를 하나씩 천천히 구현해보면 풀 수 있다.
import java.util.Arrays;
class Solution {
public int solution(String dartResult) {
int answer = 0;
int point = 0;
int arrCount = 0;
int arrIndex = -1;
char[] rec = dartResult.toCharArray();
//1. 게임은 총 3회 이루어지며, 각 기회별 점수 계산을 위한 배열을 선언.
int[] score = new int[3];
//2. 입력 받은 문자열을 구분해주기 위한 for문.
for (int i = 0; i < rec.length; i++) {
// 입력 받은 문자열을 하나씩 비교할 때, 1 다음 값이 0 이면 10점이고
// i++을 해서 다음 값 0을 건너 뛰어 다음 문자를 판독한다.
if (rec[i] >= '0' && rec[i] <= '9') {
if (rec[i] == '1' && rec[i + 1] == '0') {
point = 10;
i++;
arrIndex++;
}
else {
arrIndex++;
//정수 0의 아스키 코드 값은 48.
point = (int)rec[i] - 48;
}
//3. S(1제곱), D(2제곱), T(3제곱)
} else if (rec[i] == 'S') {
point = (int) (Math.pow(point, 1));
} else if (rec[i] == 'D') {
point = (int) (Math.pow(point, 2));
} else if (rec[i] == 'T') {
point = (int) (Math.pow(point, 3));
//4. 스타상(*), 아차상(#)
} else if (rec[i] == '*') {
//5. 1번째 기회일 경우, 본인만 2배
if (i<=3) {
point = point * 2;
}
//6. 2번째 기회 이후의 스타상일 경우
else if(i>=4) {
//본인과 본인 이전 포인트를 2배.
point = point * 2;
score[arrIndex - 1] = score[arrIndex - 1 ] * 2;
}
//7. 아차상(#)일 경우, 점수는 마이너스.
} else if (rec[i] == '#') {
point = point * -1;
}
//최종적으로 계산된 점수를 점수 배열에 넣어준다.
score[arrIndex] = point;
}
for(int i = 0; i<score.length; i++) {
answer += score[i];
}
return answer;
}
}
카카오 문제 풀어봤더니 어깨가 올라간다