프로그래머스: 정수 제곱근 판별

최창효·2022년 1월 16일
0
post-thumbnail

문제 설명

정답

파이썬

def solution(n):
    x = np.sqrt(n)
    a,b = str().split('.') #문자열 처리 후 .을 기준으로 앞과 뒤를 나눴다
    if int(b) == 0:
        return (int(x)+1)**2
    else:
        return -1

자바

class Solution {
    public long solution(long n) {
        double x  = Math.sqrt(n); 
        long answer = 0;
        if (x%1 == 0.0){
            x = (x+1) * (x+1);
            answer = (long) x;
        }else{
            answer = -1;
        }
        return answer;
    }
}

기타

double x = 1.0;
long answer = 0;
answer = (long) (x+1)*(x+1); // 에러
answer = (long) (x+1)**2; // 에러
answer = (long) ((long) (x+1)*(x+1)); // 정상작동
profile
기록하고 정리하는 걸 좋아하는 개발자.

0개의 댓글