본문 바로가기

Algorithm/Algospot

[algospot]XHAENEUNG

문제

https://algospot.com/judge/problem/read/XHAENEUNG

수학과 영어를 동시에 가르치는 학습지의 채점을 하는 프로그램을 만들어야 한다.

A operation B = C

위의 형식과같이 단순 덧셈(+),뺄셈(-),곱셈(*) 이고 A와 B는 0~10까지의 영단어(zero~ ten)가 들어온다.

다행인것은 C는 0이상 10이하의 수가 되고 그 이외에는 오답으로 처리한다. 그리고 C는 만약에 정답이 3이고 입력된 단어가 

three가 되지만 trere theer도 정답으로 인정한다. 순서가 섞여있는것까지 정답으로 인정하도록 한다.

 

입력

two + three = ivef 와 같이 공백으로 구분되는 소문자로감 구성되어있는 문자열

 

출력

맞으면 YES 틀리면 NO

 

풀이

입력된 문자열을 쪼갠뒤에 올바른 연산결과를 얻는다.

입력된 결과문자열이 올바른 답과 문자 구성이 같은지 검사한다.

 

package com.tutorial;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class XHAENEUNG {
    public static void main(String[] args)throws Exception {
        String[] numList = {
            "zero",
            "one",
            "two",
            "three",
            "four",
            "five",
            "six",
            "seven",
            "eight",
            "nine",
            "ten"
        };
        BufferedReader br = new BufferedReader(new InputStreamReader(System. in));
        int cnt = Integer.parseInt(br.readLine());
        while (cnt -- > 0) {
            String[] str = br.readLine().split(" ");
            String a = str[0];
            String op = str[1];
            String b = str[2];
            String c = str[4];
            int result = 0,
            a1 = 0,
            b1 = 0;
            for (int i = 1; i <= 10; i ++) {
                if (numList[i - 1].equals(a)) 
                    a1 = i - 1;
                
                if (numList[i - 1].equals(b)) 
                    b1 = i - 1;
                
            }
            if (op.equals("+")) {
                result = a1 + b1;
            } else if (op.equals("-")) {
                result = a1 - b1;
            } else if (op.equals("*")) {
                result = a1 * b1;
            }
            if (result < 0 || result > 10) {
                System.out.println("No");
            } else {
                char[] d = c.toCharArray();
                char[] e = (numList[result]).toCharArray();
                char temp;
                if (d.length == e.length) {
                    for (int i = 0; i < d.length; i ++) {
                        for (int j = i + 1; j < d.length; j ++) {
                            if (d[i] > d[j]) {
                                temp = d[i];
                                d[i] = d[j];
                                d[j] = temp;
                            }
                            if (e[i] > e[j]) {
                                temp = e[i];
                                e[i] = e[j];
                                e[j] = temp;
                            }
                        }
                    }
                    if (String.valueOf(e).equals(String.valueOf(d))) {
                        System.out.println("Yes");
                    } else 
                        System.out.println("No");
                    
                } else 
                    System.out.println("No");
                
            }
        }
    }
}

 

'Algorithm > Algospot' 카테고리의 다른 글

[algospot]WEIRD  (0) 2014.12.15
[algospot]URI  (0) 2014.12.15
[algospot]HOTSUMMER  (0) 2014.12.11
[algospot]CONVERT  (0) 2014.12.11
[algospot]MISPELL  (0) 2014.12.11