inblog logo
|
silver
    알고리즘문제풀기

    [알고리즘문제풀기] 두 수의 연산값 비교하기

    silver's avatar
    silver
    Mar 26, 2025
    [알고리즘문제풀기] 두 수의 연산값 비교하기
    Contents
    문제내가 작성한 정답다른 사람들의 정답

    문제

    school.programmers.co.kr
    https://school.programmers.co.kr/learn/courses/30/lessons/181938

    내가 작성한 정답

    +“”

    class Solution { public int solution(int a, int b) { int a1 = Integer.parseInt(a+""+b); int a2 = 2*a*b; return Math.max(a1,a2); } }

    String.valueOf , Integer.toString

    class Solution { public int solution(int a, int b) { int a1 = Integer.parseInt(String.valueOf(a)+Integer.toString(b)); int a2 = 2*a*b; return a1 > a2 ? a1: a2; } }

    다른 사람들의 정답

    class Solution { public int solution(int a, int b) { return Math.max( // Math.log10(b)로 10의 자리수를 구해 1을 더한 것을 10의 거듭제곱으로 계산하여 a를 곱해준다 (int)Math.pow(10, (int)Math.log10(b) + 1) * a + b, 2 * a * b ); } }
    💡
    Math.pow는 Java에서 거듭제곱을 계산하는 데 사용되는 메서드로 반환값은 double타입이다.
    double result = Math.pow(double 거듭제곱을 계산할 값, double 거듭제곱을 나타내는 지수); double result = Math.pow(-2, 3); // (-2)^3 = -8 System.out.println(result); // 출력: -8.0 double result = Math.pow(4, 0.5); // 4^(1/2) = √4 = 2 System.out.println(result); // 출력: 2.0
    Share article

    silver

    RSS·Powered by Inblog