XHAENEUNG 자꾸 오답됩니다

  • pvjkim
    pvjkim

    안녕하세요

    XHAENEUNG 를 자바 버전으로 풀려고 하는데 자꾸 오답되네요
    문제 자체는 그리 어려운 것 같지 않아서 뭔가 문제에 대한 이해가 안된것인지 고민입니다..
    제가 쓴 코드입니다.
    문제에 대한 이해가 안된것인지 아니면 로직이 잘못된 곳이 있는 것인지요..
    **java 로쓴 코드 **

    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    import java.util.Scanner;
    import java.util.StringTokenizer;

    public class Main {

    private static class AlphabetLength implements Comparable<AlphabetLength>{
        private Character alphabet;
        private int alphabetLength;
    
        public AlphabetLength(Character alphabet) {
            this.alphabet = alphabet;
            this.alphabetLength = 1;
        }
    
        public void addAlphabetCount() {
            this.alphabetLength++;
        }
    
        @Override
        public int compareTo(AlphabetLength o) {
            int alphabetCompareResult = this.alphabet.compareTo(o.alphabet);
            if (alphabetCompareResult == 0) {
                return new Integer(this.alphabetLength).compareTo(o.alphabetLength);
            }
            return alphabetCompareResult;
        }
    
        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result
                    + ((alphabet == null) ? 0 : alphabet.hashCode());
            result = prime * result + alphabetLength;
            return result;
        }
    
        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            AlphabetLength other = (AlphabetLength) obj;
            if (alphabet == null) {
                if (other.alphabet != null)
                    return false;
            } else if (!alphabet.equals(other.alphabet))
                return false;
            if (alphabetLength != other.alphabetLength)
                return false;
            return true;
        }
    }
    private static class AlphabetLengthFactory {
        private AlphabetLengthFactory() {
        }
        public static AlphabetLength[] getAlphabetLength(String numberString) {
            if (numberString == null) {
                return null;
            }
            List<AlphabetLength> alphabetLengthList = new ArrayList<AlphabetLength>();
            char ch;
            int alphabetLengthIndex;
            for (int i=0; i<numberString.length(); i++) {
                ch = numberString.charAt(i);
                alphabetLengthIndex = alphabetLengthList.indexOf(ch);
                if (alphabetLengthIndex > -1) {
                    alphabetLengthList.get(alphabetLengthIndex).addAlphabetCount();
                } else {
                    alphabetLengthList.add(new AlphabetLength(ch));
                }
            }
            return alphabetLengthList.toArray(new AlphabetLength[0]);
        }
    }
    private static enum Number {
        zero(0), one(1), two(2), three(3), four(4), five(5), six(6), seven(7), eight(8), nine(9), ten(10), outofrangenumber(-1, true);
        private AlphabetLength[] alphabetLengthArray;
        private int intValue;
        private boolean outofrangeFlag;
        private Number(int intValue) {
            this(intValue, false);
        }
        private Number(int intValue, boolean outofrangeFlag) {
            this.intValue = intValue;
            this.alphabetLengthArray = AlphabetLengthFactory.getAlphabetLength(this.name());
            Arrays.sort(this.alphabetLengthArray);
        }
        public boolean isOutofRange() {
            return this.outofrangeFlag;
        }
        public Number operate(String operatorString, Number otherOperand) {
            int result = -1;
            if (operatorString.equals("+")) {
                result = this.intValue + otherOperand.intValue;
            } else if (operatorString.equals("-")) {
                result = this.intValue - otherOperand.intValue;
            } else if (operatorString.equals("*")) {
                result = this.intValue * otherOperand.intValue;
            }
            if (result < 0 || result > 10) {
                return outofrangenumber;
            }
            return Number.values()[result];
        }
        public boolean equalsNumber(Number otherNumber) {
            return !isOutofRange() && !otherNumber.isOutofRange() && this.intValue == otherNumber.intValue;
        }
        public static Number valueOfNumberString(String numberString) {
            if (numberString == null) {
                return Number.outofrangenumber;
            }
            AlphabetLength[] otherAlphabetLengthArray = AlphabetLengthFactory.getAlphabetLength(numberString);
            for (Number number : Number.values()) {
                if (equalsAlphabetLength(number, numberString) && equalsIgnoreOrder(number, otherAlphabetLengthArray)) {
                    return number;
                }
            }
            return Number.outofrangenumber;
        }
        private static boolean equalsIgnoreOrder(Number sourceNumber, AlphabetLength[] otherAlphabetLengthArray) {
            Arrays.sort(otherAlphabetLengthArray);
            return Arrays.equals(sourceNumber.alphabetLengthArray, otherAlphabetLengthArray);
        }
        private static boolean equalsAlphabetLength(Number sourceNumber, String numberString) {
            return sourceNumber.name().length() == numberString.length();
        }
    }
    
    public static void main(String[] args) {
    
        Scanner sc = new Scanner(System.in);
        int testcase = Integer.valueOf(sc.nextLine());
        StringTokenizer st;
        Number leftOperand;
        String operator;
        Number rightOperand;
        Number answer;
        Number correctAnswer;
        while (0<testcase--) {
            st = new StringTokenizer(sc.nextLine(), " ");
            leftOperand = Number.valueOf(st.nextToken());
            operator = st.nextToken();
            rightOperand = Number.valueOf(st.nextToken());
            st.nextToken();// =
            answer = Number.valueOfNumberString(st.nextToken());
            correctAnswer = leftOperand.operate(operator, rightOperand);
            if (!answer.isOutofRange() && !correctAnswer.isOutofRange() && answer.equalsNumber(correctAnswer)) {
                System.out.println("Yes");
            } else {
                System.out.println("No");
            }
        }
    }

    }


    11년 전
2개의 댓글이 있습니다.
  • 샥후
    샥후

    zero - one = asdf와 같은 입력에서 Yes가 나옵니다.


    11년 전 link
  • pvjkim
    pvjkim

    // 샥후
    정말정말 감사합니다 기본을 빠뜨렸네요..
    다행히 통과했습니다.
    좋은하루 되세요~!!
    :)


    11년 전 link
  • 정회원 권한이 있어야 커멘트를 다실 수 있습니다. 정회원이 되시려면 온라인 저지에서 5문제 이상을 푸시고, 가입 후 7일 이상이 지나셔야 합니다. 현재 문제를 푸셨습니다.