coding study/baekjoon
[java] 1977. 완전제곱수
yeoonii
2023. 8. 1. 23:02
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
//m과 n 값 개행으로 입력받음
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int m = Integer.parseInt(br.readLine()); //시작
int n = Integer.parseInt(br.readLine()); //끝
boolean hasPerfectSquare = false;
int sum = 0;
int min = Integer.MAX_VALUE; //초기값이 최솟값보다 항상 크다고 보장하도록
for (int i = m; i <= n; i++) {
int sqrt = (int) Math.sqrt(i);
if (sqrt * sqrt == i) { //완전제곱근인 경우
hasPerfectSquare = true;
sum += i; //해당 값을 sum에 저장하고
if (i < min) { //min보다 그 값이 작은 경우
min = i; //최솟값으로 저장
}
}
}
if (hasPerfectSquare) {
System.out.println(sum);
System.out.println(min);
} else {
//완전제곱수가 없는 경우
System.out.println("-1");
}
}
}