본문 바로가기
코딩테스트/자바 Level 0

[Java] 더 크게 합치기

by onggury 2023. 7. 29.

문제

연산 ⊕는 두 정수에 대한 연산으로 두 정수를 붙여서 쓴 값을 반환합니다. 예를 들면 다음과 같습니다.

  • 12 ⊕ 3 = 123
  • 3 ⊕ 12 = 312

양의 정수 a와 b가 주어졌을 때, a  b와 b  a 중 더 큰 값을 return 하는 solution 함수를 완성해 주세요.

단, a  b와 b  a가 같다면 a  b를 return 합니다.

 

 

제한사항

  • 1 ≤ a, b < 10,000

 

class Solution {
    public int solution(int a, int b) {
        StringBuilder num1 = new StringBuilder();
        StringBuilder num2 = new StringBuilder();
        
        num1.append(a);
        num1.append(b);
        
        num2.append(b);
        num2.append(a);
        
        return Math.max(Integer.parseInt(num1.toString()), Integer.parseInt(num2.toString()));
    }
}

 

 

 

출처

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