[Programmers] 코딩테스트 입문 120907. OX 퀴즈

이지현·2023년 3월 3일
0

Algorithm

목록 보기
51/81
post-thumbnail

✔️ Problem URL

OX 퀴즈


✔️ Problem

덧셈, 뺄셈 수식들이 'X [연산자] Y = Z' 형태로 들어있는 문자열 배열 quiz가 매개변수로 주어집니다. 수식이 옳다면 "O"를 틀리다면 "X"를 순서대로 담은 배열을 return하도록 solution 함수를 완성해주세요.


✔️ Code

class Solution {
    public String[] solution(String[] quiz) {
        String[] answer = new String[quiz.length];
        
        for(int i = 0; i < quiz.length; i++) {
            String[] temp = quiz[i].split(" ");
            if(temp[1].equals("+")) {
                if(Integer.parseInt(temp[0]) + Integer.parseInt(temp[2]) == Integer.parseInt(temp[4])) {
                    answer[i] = "O";
                }
                else {
                    answer[i] = "X";
                }
            }
            else if(temp[1].equals("-")) {
                if(Integer.parseInt(temp[0]) - Integer.parseInt(temp[2]) == Integer.parseInt(temp[4])) {
                    answer[i] = "O";
                }
                else {
                    answer[i] = "X";
                }
            }
        }
        return answer;
    }
}
profile
2023.09 ~ 티스토리 이전 / 2024.04 ~ 깃허브 블로그 이전

0개의 댓글