현의 개발 블로그

[프로그래머스] 두 수의 연산값 비교하기 본문

프로그래밍 언어/자바 문제풀이

[프로그래머스] 두 수의 연산값 비교하기

hyun2371 2023. 6. 23. 18:48

문제

 

입출력

 

풀이

묵시적 형변환에 의해 String과 int 타입을 더하면 String 타입이 된다.

class Solution {
    public int solution(int a, int b) {

        int concat_ab  = Integer.parseInt(String.valueOf(a)+b);
        return Math.max(concat_ab, 2 * a * b);
    }
}

 

 

다른 풀이

max() 대신 삼항 연산자를 사용할 수 도 있다.

class Solution {
    public int solution(int a, int b) {

        int concat_ab  = Integer.parseInt(String.valueOf(a)+b);
        return (concat_ab > 2 * a * b ? concat_ab : 2 * a * b);
    }
}

 

Comments