코딩 테스트 (Java)/코딩 기초 트레이닝 (프로그래머스)

181865. 간단한 식 계산하기

가지코딩 2025. 4. 22. 14:13

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

 

프로그래머스

SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr


문제

문자열 binomial이 매개변수로 주어집니다. binomial은 "a op b" 형태의 이항식이고 ab는 음이 아닌 정수, op는 '+', '-', '*' 중 하나입니다. 주어진 식을 계산한 정수를 return 하는 solution 함수를 작성해 주세요.

 

 

제한사항

  • 0 ≤ a, b ≤ 40,000
  • 0을 제외하고 a, b는 0으로 시작하지 않습니다.

 

입출력 예

binomial result
"43 + 12" 55
"0 - 7777" -7777
"40000 * 40000" 1600000000

 

입출력 예 #1

  • 예제 1번의 binomial은 "43 + 12"로 이 식을 계산한 결과인 43 + 12 = 55를 return 합니다.

입출력 예 #2

  • 예제 2번의 binomial은 "0 - 7777"로 이 식을 계산한 결과인 0 - 7777 = -7777을 return 합니다.

입출력 예 #3

  • 예제 3번의 binomial은 "40000 * 40000"으로 이 식을 계산한 결과인 40000 × 40000 = 1600000000을 return 합니다.

풀이

class Solution {
    public int solution(String binomial) {
        String[] arr = binomial.split(" ");
        
        int a = Integer.parseInt(arr[0]);
        int b = Integer.parseInt(arr[2]);
        char op = arr[1].charAt(0);
        
        int answer = switch(op){
            case '+' -> a + b;
            case '-' -> a - b;
            case '*' -> a * b;
            default -> 0;
        };
        
        return answer;
    }
}

 

 

 

 


GitHub: https://github.com/gajicoding/coding_test/edit/main/%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%98%EB%A8%B8%EC%8A%A4/0/181865.%E2%80%85%EA%B0%84%EB%8B%A8%ED%95%9C%E2%80%85%EC%8B%9D%E2%80%85%EA%B3%84%EC%82%B0%ED%95%98%EA%B8%B0

 

coding_test/프로그래머스/0/181865. 간단한 식 계산하기 at main · gajicoding/coding_test

This is an auto push repository for Baekjoon Online Judge created with [BaekjoonHub](https://github.com/BaekjoonHub/BaekjoonHub). - gajicoding/coding_test

github.com


출처: 프로그래머스 코딩 테스트 연습, https://school.programmers.co.kr/learn/challenges

 

코딩테스트 연습 | 프로그래머스 스쿨

개발자 취업의 필수 관문 코딩테스트를 철저하게 연습하고 대비할 수 있는 문제를 총망라! 프로그래머스에서 선발한 문제로 유형을 파악하고 실력을 업그레이드해 보세요!

school.programmers.co.kr