728x90

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

 

프로그래머스

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

programmers.co.kr

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

 

 

 

 

 

풀이)

class Solution {
    public String[] solution(String[] quiz) {
        String[] answer = new String[quiz.length];

        for(int i=0; i< quiz.length; i++){
            String a = quiz[i];
            a = a.replace(" + ", "p");
            a = a.replace(" - ", "m");
            a = a.replace(" = ", "r");
            a = a.trim();


            if(a.indexOf("p")>0){
                int x = a.indexOf("p");
                int y = a.indexOf("r");
                int x1 = Integer.parseInt(a.substring(0,x));
                int x2 = Integer.parseInt(a.substring(x+1 , y));
                int ans = Integer.parseInt(a.substring(y+1, a.length()));
                if(x1+x2==ans){
                    answer[i]="O";
                }else{
                    answer[i]="X";
                }
            }else if(a.indexOf("m")>0){
                int x = a.indexOf("m");
                int y = a.indexOf("r");
                int x1 = Integer.parseInt(a.substring(0,x));
                int x2 = Integer.parseInt(a.substring(x+1, y));
                int ans = Integer.parseInt(a.substring(y+1, a.length()));
                if(x1-x2==ans){
                    answer[i]="O";
                }else{
                    answer[i]="X";
                }
            }


        }
        return answer;
    }
}

공백을 이용해서 푸는 좀 더 간단한 방법이다.

다른 방법으로 푼 방법은 아래 링크를 참조바란다.

https://lavenderje.tistory.com/208

 

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

https://school.programmers.co.kr/learn/courses/30/lessons/120907 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞

lavenderje.tistory.com

 

728x90