웹/JSP
프로그래머스-level1-다트 게임(카카오)
송민준
2019. 11. 11. 16:33
https://programmers.co.kr/learn/courses/30/lessons/17682
코딩테스트 연습 - [1차] 다트 게임 | 프로그래머스
programmers.co.kr
카카오 난이도(하) 문제.
1. 점수 부분과 제곱 부분, 옵션부분 구역을 따로따로 정해준다.
2. String 한문장씩 나눠서 각 구역에 맞게 값을 넣어준다.
3. 마지막에 계산 후 더한다.
* 숫자타입인지 확인하는 메소드는 따로 만들어줬음.
public int solution(String dartResult) {
int sum =0;
String[] jumsu = {"","",""};
int[] jumSqrt = {0, 0, 0};
int[] option = {1, 1, 1};
int jumsuIndex = 0;
for(int i =0; i < dartResult.length(); i++) {
String ch = Character.toString(dartResult.charAt(i));
if(isNum(ch)) {
jumsu[jumsuIndex] += ch;
} else {
if(ch.equals("S")) {
jumSqrt[jumsuIndex] = 1;
jumsuIndex++;
} else if(ch.equals("D")) {
jumSqrt[jumsuIndex] = 2;
jumsuIndex++;
} else if(ch.equals("T")){
jumSqrt[jumsuIndex] = 3;
jumsuIndex++;
} else if(ch.equals("*")) {
option[jumsuIndex-1] = 2;
if(jumsuIndex > 1) {
option[jumsuIndex-2] *= 2;
}
} else {
option[jumsuIndex-1] = -1;
}
}
}
for(int i=0; i < 3; i++) {
sum += Math.pow(Integer.parseInt(jumsu[i]),jumSqrt[i])*option[i];
System.out.println("점수 : " + jumsu[i] + " 제곱 : " + jumSqrt[i] + " 옵션 : " +option[i]);
System.out.println("합계 : " + sum);
}
return sum;
}
public static boolean isNum(String s) {
try {
Integer.parseInt(s);
return true;
} catch(NumberFormatException e) {
return false;
}
}