링크 : https://www.acmicpc.net/problem/1011
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
for (int i = 0; i < T; i++) {
String[] input = br.readLine().split(" ");
int x = Integer.parseInt(input[0]);
int y = Integer.parseInt(input[1]);
int distance = y - x;
int max = (int) Math.sqrt(distance);
if (max == Math.sqrt(distance)) {
System.out.println(2 * max - 1);
} else if (distance <= max * (max + 1)) {
System.out.println(2 * max);
} else {
System.out.println(2 * max + 1);
}
}
}
}
- max 값이 거리의 제곱근과 동일한 경우: 이동 횟수는 2 * max - 1입니다.
- 거리가 max (max + 1)보다 작거나 같은 경우: 이동 횟수는 2 max입니다.
- 그렇지 않은 경우(거리가 max (max + 1)보다 큰 경우): 이동 횟수는 2 max + 1입니다.
결국, 수추리 & 계차수열의 문제인 것 같습니다.
참고로, 여기서 키 포인트는 "2" 입니다.
필자도 해당문제에 대해, 처음부터 이해를 하진 못했고,
해당 문제를 풀기 위해 참고 및 학습하였던 블로그의 출처를 남기겠습니다.
출처 : https://st-lab.tistory.com/79