Coding Test/프로그래머스[JAVA]
[프로그래머스 Lv0.] 120909번 제곱수 판별하기 (JAVA)
woojin._.
2023. 7. 20. 10:52

1. 문제
문제 설명
어떤 자연수를 제곱했을 때 나오는 정수를 제곱수라고 합니다. 정수 n이 매개변수로 주어질 때, n이 제곱수라면 1을 아니라면 2를 return하도록 solution 함수를 완성해주세요.
제한사항
- 1 ≤ n ≤ 1,000,000
입출력 예
n | result |
144 | 1 |
976 | 2 |
2. 풀이과정
[첫 번째 방법]
import static java.lang.Math.*;
class Solution {
public int solution(int n) {
int answer = 0;
long s = round(sqrt(n));
if(s*s == n) {
answer = 1;
} else {
answer = 2;
}
return answer;
}
}
[두 번째 방법]
class Solution {
public int solution(int n) {
return (n % Math.sqrt(n) == 0 ? 1 : 2);
}
}