코테/프로그래머스
프로그래머스 [1차] 다트게임
65살까지 코딩
2022. 1. 17. 01:11
728x90
반응형
import java.util.ArrayList;
class Solution {
public int solution(String dartResult) {
int answer = 0;
char[] chars = dartResult.toCharArray();
ArrayList<String> arrayList = new ArrayList<>();
int i = 0;
//char단위로 쪼개서 문자열로 바꿔서 배열에 넣어주는 과정
while (chars.length != i) {
String str = "";
//"1D2X";
if (Character.isDigit(chars[i])) {
//10처리
if (chars[i] == '1' && chars[i + 1] == '0') {
str += chars[i];
i++;
str += chars[i];
i++;
} else {
str += chars[i];
i++;
}
if (chars.length == i) {
break;
}
while (!Character.isDigit(chars[i])) {
str += chars[i];
i++;
if (chars.length == i) {
break;
}
}
}
arrayList.add(str);
}
int[] scoreSet = new int[arrayList.size()];
ArrayList<Integer> arrayList1 = new ArrayList<>();
int k = 0;
int tmp = 0;
System.out.println(arrayList);
for (String s : arrayList) {
//여기서 부턴 그냥 로직떄로 따라가면 됨
char[] chars1 = s.toCharArray();
for (int j = 0; j < chars1.length; j++) {
System.out.println(chars1[j]);
//10 처리만 잘해주면됨
if (Character.isDigit(chars1[j]) && Character.isDigit(chars1[j + 1])) {
tmp = Integer.parseInt(String.valueOf(chars1[j]) + String.valueOf(chars1[j + 1]));
scoreSet[k] = tmp;
j++;
k++;
//10이 아닌 숫자
} else if (Character.isDigit(chars1[j])) {
tmp = Integer.parseInt(String.valueOf(chars1[j]));
scoreSet[k] = tmp;
k++;
// 문제조건 따라가면 되는 부분들
} else if (chars1[j] == 'D') {
scoreSet[k-1] = scoreSet[k-1] * scoreSet[k-1];
} else if (chars1[j] == 'T') {
scoreSet[k-1] = scoreSet[k-1] * scoreSet[k-1] * scoreSet[k-1];
} else if (chars1[j] == '*') {
// 첫번째 수일 때
if(k == 1) {
scoreSet[k - 1] = scoreSet[k-1] * 2;
}else {
scoreSet[k - 2] = scoreSet[k-2] * 2;
scoreSet[k-1] = scoreSet[k-1] * 2;
}
} else if (chars1[j] == '#') {
scoreSet[k - 1] = -scoreSet[k - 1];
}
}
}
for (int i1 : scoreSet) {
answer += i1;
}
return answer;
}
}
문제 : https://programmers.co.kr/learn/courses/30/lessons/17682
코딩테스트 연습 - [1차] 다트 게임
programmers.co.kr
728x90
반응형