SGU 111

就是给你一个1000位以下的数。。求他的平方根(round down)。。
我由于是java的。。但是java中biginteger没有sqrt的功能。。我只好用二分查找了。。
如果位数是n的话。。二分是n的。。乘法是nlogn的。。所以总共就是n^2logn的。
其实是有10n的好办法的。。
不过我太懒了。。
Code:
import java.util.*;
import java.math.*;
public class Main
{
static Scanner in=new Scanner(System.in);
public static void main(String args[])
{
BigInteger l,r,m,x,tmp;
l=BigInteger.valueOf(0);x=in.nextBigInteger();
r=x.abs().add(BigInteger.ONE);
while(l.add(BigInteger.ONE).compareTo(r)==-1)
{
m=l.add(r).divide(BigInteger.valueOf(2));
if(m.multiply(m).compareTo(x)!=1)
l=m;
else
r=m;
}
System.out.println(l);
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *