BOJ 13706. 제곱근
binary-search
- BigInteger 사용
- binary search로 제곱근 탐색
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import java.io.*;
import java.util.*;
import java.math.BigInteger;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
BigInteger dest = new BigInteger(in.readLine());
BigInteger two = BigInteger.valueOf(2);
BigInteger left = BigInteger.valueOf(1);
BigInteger right = dest;
while (left.compareTo(right) <= 0) {
BigInteger mid = left.add(right).divide(two);
BigInteger square = mid.multiply(mid);
if (square.equals(dest)) {
System.out.println(mid);
break;
} else if (square.compareTo(dest) < 0)
left = mid.add(BigInteger.ONE);
else
right = mid.subtract(BigInteger.ONE);
}
}
}