UVA - 10023 Square root

题目大意:给出一个出,然后你求他的开方,这个数最大为10^1000。


解题思路:用二分搜索找到他的开方


import java.io.*;
import java.math.*;
import java.util.*;

public class Main {
	public static void main(String []arg) {
		Scanner cin = new Scanner(System.in);
		int n = cin.nextInt();
		for (int i = 0; i < n; i++) {
			BigInteger m, x, y;
			m = cin.nextBigInteger();
			y = m;
			do {
				x = y;
				y = x.add(m.divide(x)).divide(BigInteger.valueOf(2));
			} while (x.compareTo(y) == 1);

			System.out.println(x);
			if (i < n - 1)
				System.out.println();
		}
	}
}


你可能感兴趣的:(UVA - 10023 Square root)