Contents
문제 풀기

문제 풀기
class Solution {
public int solution(int a, int b) {
int ab = Integer.parseInt("" + a + b);
int ba = Integer.parseInt("" + b + a);
int answer = Math.max(ab, ba);
return answer;
}
}
int ab = Integer.parseInt("" + a + b);
"" + a
→ 숫자a
를 문자열로 변환.("" + a) + b
→ 문자열로 변환된a
뒤에b
를 이어붙임.- 예: a=12, b=3 →
"" + 12 + 3
→"12" + "3"
→"123"
. Integer.parseInt("123")
→ 정수123
으로 변환.- 즉, a 뒤에 b를 붙인 숫자
int answer = Math.max(ab, ba);
- 두 수 중 더 큰 값을
answer
에 저장.
return answer;
- 최종 결과 반환
Math.max
Math.max()
는 두 값 중 더 큰 값을 반환하는 자바 표준 라이브러리 메서드📌 기본 문법
java 복사편집 Math.max(x, y)
- x, y: 비교할 두 값
- 반환값: x와 y 중 더 큰 값 (같으면 x 반환)
📌 지원 타입
Math.max
는 여러 오버로딩이 있어서 정수, 실수 모두 쓸 수 있다.Math.max(int a, int b)
Math.max(long a, long b)
Math.max(float a, float b)
Math.max(double a, double b)
📌 예시
System.out.println(Math.max(10, 20)); // 20
System.out.println(Math.max(-5, -10)); // -5
System.out.println(Math.max(3.14, 2.71)); // 3.14
System.out.println(Math.max(5.0f, 5.0f)); // 5.0
📌 동작 방식
- 내부적으로는 비교 연산(
>
)을 해서 큰 값을 그대로 반환.
- 두 값이 같으면 첫 번째 인자를 반환.
Share article