自写sqrt
如题= =自用模板
Math.sqrt();当传进去为long时会有误差 这点和C++一样 所以自己手写了个二分的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
static long sqrt(long n){  
long start=1;
long end=n;
long res=-1;
while(start<=end){
long mid=(start+end)/2;
if(BigInteger.valueOf(mid).multiply(BigInteger.valueOf(mid)).compareTo(BigInteger.valueOf(n))<=0){
res=mid;
start=mid+1;
}else{
end=mid-1;
}
}
return res;
}