알고리즘
[백준, 자바, 1977번] 완전제곱수
hminor
2024. 11. 10. 19:24
반응형
풀이
- 해당 문제는 간단한 문제로
- 문제만 잘 읽고, 이해하면 되는 문제라고 판단하여 빠르게 해결
- M과 N 사이의 값 중 완전제곱인 수의 합과, 최솟값을 구하는 문제로
- M을 루트 씌운 값을 시작으로 완전제곱일 경우에 대한 조건 분기를 거쳐 해결.
import java.util.Scanner;
public class _1977 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int M = sc.nextInt();
int N = sc.nextInt();
long result = 0;
int now = (int)Math.round(Math.sqrt(M));
int start = 0;
while (now*now<=N) {
if (M<=now*now&&now*now<=N) {
result+= (long) now*now;
if (start==0) start = now*now;
}
now++;
}
if (result==0) System.out.println(-1);
else {
System.out.println(result);
System.out.println(start);
}
}
}