We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent a0e3634 commit fe04a81Copy full SHA for fe04a81
03do-new30/202503/05 BOJ G3 이진트리.md
@@ -0,0 +1,33 @@
1
+```java
2
+import java.util.*;
3
+
4
+public class Main {
5
+ static int K;
6
+ public static void main(String[] args) {
7
+ Scanner sc = new Scanner(System.in);
8
+ long N = sc.nextLong();
9
+ K = sc.nextInt();
10
+ int Q = sc.nextInt();
11
+ for (int i = 0; i < Q; i++) {
12
+ long x = sc.nextLong() - 1;
13
+ long y = sc.nextLong() - 1;
14
+ if (K == 1) {
15
+ System.out.println(Math.abs(x - y));
16
+ } else {
17
+ int dist = 0;
18
+ while (x != y) {
19
+ if (x < y) {
20
+ y = (y - 1) / K; // y = y의 부모 노드
21
22
+ x = (x - 1) / K; // x = x의 부모 노드
23
+ }
24
+ dist++;
25
26
+ System.out.println(dist);
27
28
29
+ sc.close();
30
31
+}
32
33
+```
0 commit comments