본문 바로가기

Java/프로그래머스

[JAVA] 프로그래머스 - OX퀴즈

https://school.programmers.co.kr/learn/courses/30/lessons/120907

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

 

 

 

정답 

 

import java.util.ArrayList;

class Solution {
    public String[] solution(String[] quiz) {
        ArrayList<String> list = new ArrayList<String>();
        
        for(int i=0; i<quiz.length; i++)
        {
            //공백을 기준으로 spilt
            String strarr[] = quiz[i].split(" ");
            
            int a = Integer.parseInt(strarr[0]);
            int b = Integer.parseInt(strarr[2]);
            String op = strarr[1];
            int result = Integer.parseInt(strarr[4]);
            
            if(op.equals("+"))
            {
                int temp = a+b;
                if(temp==result)
                {
                    list.add("O");
                }
                else 
                {
                    list.add("X");
                }
            }
            else if(op.equals("-"))
            {
                int temp = a-b;
                if(temp==result)
                {
                    list.add("O");
                }
                else
                {
                    list.add("X");
                }
            }           
        }
        
        String answer[] = new String[list.size()];
        for(int i=0; i<list.size(); i++)
        {
            answer[i] = list.get(i);
        }
        
        return answer;
    }
}