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

181933. flag에 따라 다른 값 반환하기

가지코딩 2025. 4. 2. 15:00

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

 

프로그래머스

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

programmers.co.kr


문제

두 정수 a, b와 boolean 변수 flag가 매개변수로 주어질 때, flag가 true면 a + b를 false면 a - b를 return 하는 solution 함수를 작성해 주세요.

 

제한사항

  • -1,000 ≤ a, b ≤ 1,000

 

입출력 예

a b flag result
-4 7 true 3
-4 7 false -11

 


풀이

class Solution {
    public int solution(int a, int b, boolean flag) {
        int answer = flag ? a+b : a-b;
        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/181933.%E2%80%85flag%EC%97%90%E2%80%85%EB%94%B0%EB%9D%BC%E2%80%85%EB%8B%A4%EB%A5%B8%E2%80%85%EA%B0%92%E2%80%85%EB%B0%98%ED%99%98%ED%95%98%EA%B8%B0

 

coding_test/프로그래머스/0/181933. flag에 따라 다른 값 반환하기 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