C# 알고리즘 코드카타

19. 정수 제곱근 판별

잼잼재미 2023. 11. 7. 09:27

public class Solution {
    public long solution(long n) {
        long x;
        
        for (int i = 1; i <= n; ++i)
        {
            x = i;
            if(x * x == n) return (x + 1)*(x + 1);
            else if (x * x > n) break;
        }
                
        return -1;
    }
}

* n = 1일 경우도 생각해야 함