UVA_10023
今天学了一下手算开平方的算法,其实也没我之前想的那么麻烦,因为懒得敲C++的大数模块,就直接用Java写了。
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void solve(BigInteger y)
{
int i, j;
BigInteger x = new BigInteger("0");
String str = "0" + y.toString();
BigInteger dvs = new BigInteger("0"), rmd = new BigInteger("0");
i = str.length() % 2;
for(; i < str.length(); i += 2)
{
rmd = rmd.multiply(BigInteger.valueOf(100)).add(new BigInteger(str.substring(i, i + 2)));
dvs = x.multiply(BigInteger.valueOf(20));
for(j = 0; j < 10; j ++)
{
if(dvs.add(BigInteger.valueOf(j + 1)).multiply(BigInteger.valueOf(j + 1)).compareTo(rmd) == 1)
{
rmd = rmd.add(dvs.add(BigInteger.valueOf(j)).multiply(BigInteger.valueOf(j)).negate());
x = x.multiply(BigInteger.valueOf(10)).add(BigInteger.valueOf(j));
break;
}
}
}
System.out.println(x);
}
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int t = 0, tt;
t = cin.nextInt();
for(tt = 0; tt < t; tt ++)
{
if(tt != 0)
System.out.println();
solve(cin.nextBigInteger());
}
}
}