-
프로그래머스 - 다트 게임 [2018 KAKAO BLIND RECRUITMENT]알고리즘 2022. 6. 28.반응형
2018 KAKAO BLIND RECRUITMENT 풀어야 할 로직 부분 입출력 예제 내 풀이
public class P_30 {public int solution(String dartResult) {// dart gameint answer = 0;char [] arr = dartResult.toCharArray();int [] round = new int[3]; // 3개의 라운드int idx = -1;for(int i=0; i<arr.length; i++) {if(Character.isDigit(arr[i])) { // i가 숫자라면idx++;if(arr[i] == '0' && i == 0) { // 스타트 0 체크} else if(arr[i] == '0' && Character.isDigit(arr[i-1])) {--idx; // 0 이전에 넣었던 숫자가 있는 배열로 다시 이동round[idx] = 10; // 배열 값 덮어 씌우기continue;}round[idx] = Character.getNumericValue(arr[i]); // 숫자를 배열에 담음} else if(arr[i] == 'S') { // 계산 불필요continue;} else if(arr[i] == 'D') { // 2제곱round[idx] = (int) Math.pow(round[idx], 2);} else if(arr[i] == 'T') { // 3제곱round[idx] = (int) Math.pow(round[idx], 3);} else if(arr[i] == '*') { // 이전 점수, 현재 점수 x2if(idx > 0) {round[idx-1] *= 2;round[idx] *= 2;} else if(idx <= 0) {round[idx] *= 2;}} else if(arr[i] == '#') { // 현재 점수 * -1round[idx] *= -1;}}// answer = round[0] + round[1] + round[2];answer = Arrays.stream(round).sum();return answer;}public static void main(String[] args) {P_30 p = new P_30();String x = "0D2S0T";System.out.println(p.solution(x));}}문제 설명에 나와있는 규칙들 말고도 조심해야 할 점.
1. 첫번째 점수 시작이 0인 경우
2. 점수가 0~9가 아니라 10점일 경우
3. 배열로 풀 경우 잘못된 배열 크기 계산의 런타임 에러
위 3가지를 추가적으로 더 고려해서 풀어야 한다.
다른 사람 풀이
import java.util.*;class Solution {public int solution(String dartResult) {Stack<Integer> stack = new Stack<>();int sum = 0;for (int i = 0; i < dartResult.length(); ++i) {char c = dartResult.charAt(i);if (Character.isDigit(c)) {sum = (c - '0');if (sum == 1 && i < dartResult.length() - 1 && dartResult.charAt(i + 1) == '0') {sum = 10;i++;}stack.push(sum);} else {int prev = stack.pop();if (c == 'D') {prev *= prev;} else if (c == 'T') {prev = prev * prev * prev;} else if (c == '*') {if (!stack.isEmpty()) {int val = stack.pop() * 2;stack.push(val);}prev *= 2;} else if (c == '#') {prev *= (-1);}// System.out.println(prev);stack.push(prev);}}int totalScore = 0;while (!stack.isEmpty()) {totalScore += stack.pop();}return totalScore;}}다른 사람 풀이 - 2
import java.util.Stack;class Solution {public int solution(String dartResult) {Stack<Integer> stack = new Stack<>();stack.push(0);for (int a = 0; a < dartResult.length(); a++) {if (dartResult.charAt(a) == '#') {int num = stack.pop();stack.push(num*-1);}else if (dartResult.charAt(a) == '*') {int num = stack.pop();int num2 = stack.pop();stack.push(num2*2);stack.push(num*2);}else if (dartResult.charAt(a) == 'S') {int num = stack.pop();stack.push(num);}else if (dartResult.charAt(a) == 'D') {int num = stack.pop();stack.push(num*num);}else if (dartResult.charAt(a) == 'T') {int num = stack.pop();stack.push(num*num*num);}else {int num = Integer.parseInt(String.valueOf(dartResult.charAt(a)));char num2 = dartResult.charAt(a+1);if(num==1 && num2=='0') {stack.push(10);a=a+1;}else {stack.push(Integer.parseInt(String.valueOf(dartResult.charAt(a))));}}}int answer = stack.pop()+stack.pop()+stack.pop();return answer;}}다른 사람 풀이 - 패턴으로 푼 경우
import java.util.*;import java.util.regex.Matcher;import java.util.regex.Pattern;public class Solution {static int[] sum = new int[3];static int solution(String msg){String reg = "([0-9]{1,2}[S|T|D][*|#]{0,1})";Pattern p = Pattern.compile(reg+reg+reg);Matcher m = p.matcher(msg);m.find();for(int i=1; i<=m.groupCount(); i++){Pattern p1 = Pattern.compile("([0-9]{1,2})([S|T|D])([*|#]{0,1})");Matcher m1 = p1.matcher(m.group(i));m1.find();sum[i-1] = (int)Math.pow(Integer.parseInt(m1.group(1)), getpow(m1.group(2)));setting(i,m1.group(3));}return(sum[0]+ sum[1]+ sum[2]);}static void setting(int idx, String msg){if(msg.equals("*")){sum[idx - 1] *= 2;if(idx > 1 ){sum[idx - 2] *=2;}}else if(msg.equals("#")){sum[idx - 1] *=-1 ;}}static int getpow(String mag){if(mag.equals("S")){return 1;}else if(mag.equals("D")){return 2;}else {return 3;}}}반응형'알고리즘' 카테고리의 다른 글
프로그래머스 - 최소직사각형 (0) 2022.06.30 프로그래머스 - 부족한 금액 계산하기 (0) 2022.06.29 프로그래머스 - 가운데 글자 가져오기 (0) 2022.06.26 프로그래머스 - 같은 숫자는 싫어 (0) 2022.06.14 프로그래머스 - 나누어 떨어지는 숫자 배열 (0) 2022.06.13