Algorithm Study
[Java Algorithm] 프로그래머스 Lv.0 _ 두 수의 연산값 비교하기
microsaurs
2024. 3. 8. 10:03
[문제]
연산 ⊕는 두 정수에 대한 연산으로 두 정수를 붙여쓴 값을 반환합니다. 예를 들면 다음과 같습니다.
12 ⊕ 3 = 123
3 ⊕ 12 = 312
양의 정수 a와 b가 주어졌을 때, a⊕b 와 2 * a * b 중 더 큰 값을 return하는 solution 함수를 완성해주세요.
단, a⊕b 와 2 * a * b 가 같다면 a⊕b 를 return 합니다.
[Algorithm]
형변환에 관련된 문제
정수와 문자열의 형태를 바꾸는 메서드 다양하게 활용해보기
1. Integer.toString(Int형) (정수 ➡️ 문자열)
2. String.valueOf(int형) (정수 ➡️ 문자열)
3. Integer.parseInt(string형) (문자열 ➡️정수)
[Code]
// 형변환 Integer.parseInt() / toString()
class Solution1 {
public int solution(int a, int b) {
int i = Integer.parseInt(Integer.toString(a) + Integer.toString(b));
int j = 2 * a * b;
return i >= j ? i : j;
}
}
// 형변환 Integer.parseInt() / 문자열 + 정수 = 문자열
class Solution2 {
public int solution(int a, int b) {
int i = Integer.parseInt(""+a+b);
int j = 2 * a * b;
return i >= j ? i : j;
}
}
[+ Plus]
직전에 풀었던 더크게 합치기 문제랑 거의 유사 !
⬇️ 아래 링크 참고