SGU_111
又一次写大数开平方了,由于Java写起来比较方便,所以就直接用Java写了。
import java.math.BigInteger;
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
BigInteger X;
while(cin.hasNext())
{
X = cin.nextBigInteger();
System.out.println(Sqrt(X));
}
}
public static BigInteger Sqrt(BigInteger n)
{
int i, k;
BigInteger div, ans, c;
String s = "0" + n.toString();
if(s.length() % 2 == 0)
i = 1;
else
i = 2;
ans = new BigInteger("0");
c = new BigInteger("0");
for(;i < s.length(); i += 2)
{
div = ans.multiply(BigInteger.valueOf(20));
c = c.multiply(BigInteger.valueOf(100)).add(new BigInteger(s.substring(i - 1, i + 1)));
for(k = 0; k < 10; k ++)
if(div.add(BigInteger.valueOf(k + 1)).multiply(BigInteger.valueOf(k + 1)).compareTo(c) == 1)
break;
ans = ans.multiply(BigInteger.valueOf(10)).add(BigInteger.valueOf(k));
c = c.add(div.add(BigInteger.valueOf(k)).multiply(BigInteger.valueOf(k)).negate());
}
return ans;
}
}