Print
카테고리: [ Development ]
조회수: 1452

급하게 푸느라 허접하네요.

문제는 http://tech.kakao.com/2017/09/27/kakao-blind-recruitment-round-1/ 의 두번째 문제입니다.

import java.util.ArrayList;
import java.util.List;

public class DartGame {
        List<Integer> score = new ArrayList<Integer>();
        List<String> bonus = new ArrayList<String>();
        List<Integer> option = new ArrayList<Integer>();

        static int sum = 0;

        public static void main(String[] args) {
                DartGame dartgame = new DartGame();
                String[] questions = { "1S2D*3T", "1D2S#10S", "1D2S0T", "1S*2T*3S", "1D#2S*3S", "1T2D3D#", "1D2S3T*" };
                for (String s : questions) {
                        dartgame.parse(s);
                        dartgame.calc();
                        System.out.println("* " + s + " = " + sum);
                }
        }

        private void parse(String s) {
                score.clear();
                bonus.clear();
                option.clear();
                sum = 0;

                int point = 0;

                while (point < s.length()) {
                        if (s.charAt(point) == '1' && s.charAt(point + 1) == '0') {
                                score.add(Integer.parseInt(s.substring(point, point + 2)));
                                point += 2;
                        } else {
                                score.add(Integer.parseInt(s.charAt(point) + ""));
                                point++;
                        }

                        bonus.add(s.charAt(point) + "");
                        point++;

                        if (point < s.length()) {
                                if ( s.charAt(point) == '*' ) {
                                        option.add(2);
                                        point++;
                                } else if ( s.charAt(point) == '#' ) {
                                        option.add(-1);
                                        point++;
                                } else {
                                        option.add(1);
                                }

                                if ( option.size() > 1 && option.get(option.size()-1) == 2 ) {
                                        option.set(option.size() - 2, option.get(option.size() - 1) * option.get(option.size() - 2));
                                }
                        }

                        if ( score.size() - option.size() == 1 ) {
                                option.add(1);
                        }
                }
        }

        private void calc() {
                for (int idx = 0; idx < score.size(); idx++) {
                        int myScore = score.get(idx);
                        boolean isPlus = true;

                        if (bonus.get(idx).equals("D"))
                                myScore = (int) Math.pow(myScore, 2);
                        else if (bonus.get(idx).equals("T"))
                                myScore = (int) Math.pow(myScore, 3);

                        myScore *= option.get(idx);

                        if (isPlus)
                                sum += myScore;
                        else
                                sum -= myScore;
                }
        }
}