반응형

class Solution {
public int solution(int a, int b) {
int answer = 0;
String strA = Integer.toString(a);
String strB = Integer.toString(b);
String strAB = strA + strB;
String strBA = strB + strA;
int ab = Integer.parseInt(strAB);
int ba = Integer.parseInt(strBA);
if(ab >= ba) answer = ab;
else answer = ba;
return answer;
}
}
다른 풀이
class Solution {
public int solution(int a, int b) {
int answer = 0;
int aLong = Integer.parseInt(""+a+b);
int bLong = Integer.parseInt(""+b+a);
answer = aLong > bLong ? aLong : bLong;
return answer;
}
}
a > b ? a : b;
class Solution {
public int solution(int a, int b) {
return Math.max(Integer.parseInt(a + "" + b), Integer.parseInt(b + "" + a));
}
}
Math.max(a,b);
class Solution {
public int solution(int a, int b) {
int answer = 0;
// a, b의 자릿수 구하기
int lengthA = (int) (Math.log10(a)+1);
int lengthB = (int) (Math.log10(b)+1);
// a+b, b+a 구하기
int addAB = (int) (a * Math.pow(10, lengthB) + b);
int addBA = (int)(b * Math.pow(10, lengthA) + a);
// 값 비교
if(addAB >= addBA){
answer = addAB;
}
else if(addAB < addBA){
answer = addBA;
}
return answer;
}
}
Math.log10()
Math.pow()
반응형
'코딩테스트' 카테고리의 다른 글
| [프로그래머스/JAVA] 배열 두 배 만들기 (0) | 2025.07.16 |
|---|---|
| [프로그래머스/JAVA] 두 수의 연산값 비교하기 (0) | 2025.07.16 |
| [프로그래머스/JAVA] 문자열 곱하기 (0) | 2025.07.16 |
| [프로그래머스/JAVA] 문자 리스트를 문자열로 변환하기 (0) | 2025.07.16 |
| [프로그래머스/JAVA] 문자열 섞기 (0) | 2025.07.16 |