Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions 03do-new30/202503/05 BOJ G3 이진트리.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
```java
import java.util.*;

public class Main {
static int K;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
long N = sc.nextLong();
K = sc.nextInt();
int Q = sc.nextInt();
for (int i = 0; i < Q; i++) {
long x = sc.nextLong() - 1;
long y = sc.nextLong() - 1;
if (K == 1) {
System.out.println(Math.abs(x - y));
} else {
int dist = 0;
while (x != y) {
if (x < y) {
y = (y - 1) / K; // y = y의 부모 노드
} else {
x = (x - 1) / K; // x = x의 부모 노드
}
dist++;
}
System.out.println(dist);
}
}
sc.close();
}
}

```