https://www.acmicpc.net/problem/1484
현재몸무게 = x , 기억몸무게 = y 라고 가정했을 때 주어지는 G는
💡 G가 나오는 값이 없을 경우도 있으므로, boolean 변수 flag 하나 생성
값이 한 번이상 출력 되었다면 flag = true
❓ 나는 start와 end값을 어떻게 잡아야 하나 고민했다.
단순했다... 최저 몸무게가 1 이므로 start=1 , end=2 로 시작해서
점점 늘려가면 되는 것이었다
import java.io.*;
import java.util.*;
public class Main {
static int G;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
G = Integer.parseInt(br.readLine());
//x^2-y^2 = G^2 인 경우의 수
int start = 1;
int end = 2;
boolean flag = false;
while(end<100000){
int startWeight = start * start;
int endWeight = end * end;
if(endWeight - startWeight == G){
System.out.println(end);
flag = true;
}
if(endWeight - startWeight >= G){
start += 1;
}else{
end += 1;
}
}
if(!flag) System.out.println(-1);
}
}