더 크게 합치기

김윤하·2023년 12월 11일
0

CodingTest

목록 보기
14/27
post-thumbnail

문제 설명

연산 ⊕는 두 정수에 대한 연산으로 두 정수를 붙여서 쓴 값을 반환합니다. 예를 들면 다음과 같습니다.
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) {
        int answer = 0;
        String str1 = Integer.toString(a);
        str1 += Integer.toString(b);
        
        String str2 = Integer.toString(b);
        str2 += Integer.toString(a);
        
        if(Integer.parseInt(str1)>Integer.parseInt(str2))
            answer = Integer.parseInt(str1);
        else
            answer = Integer.parseInt(str2);
            
        
        return answer;
    }
}
  • a와 b을 string으로 변환하고 a와 b를 더한 값을 str1에 넣고, b와 a를 더한 값을 str2에 넣음
  • if문에서 str1과 str2을 int형으로 변환하여 비교한다.

0개의 댓글