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

김엄지·2024년 3월 16일

알고리즘

목록 보기
16/90

🐤 목표

앞으로 매일 꾸준히 코딩테스트를 진행하면서 단계를 높여가보자.

문제 설명

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

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

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

제한사항

1 ≤ a, b < 10,000

문제 풀이

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

        String str = Integer.toString(a) + Integer.toString(b);
        int ab = Integer.valueOf(str);
        
        if (ab >= 2*a*b) {
        	return ab;
        } else {
        	return 2*a*b;
        }
    }
}

❗형변환

int -> string 와 string -> int

int -> string 형변환

  • Integer.toString(int값)
  • String.valueOf(int값)

string -> int 형변환

  • Integer.valueOf(String값)
profile
나만의 무언가를 가진 프로그래머가 되자

0개의 댓글