【常见面试算法】二分法开根号
public class 二分法开根号 { public static void main(String[] args) { System.out.println(find(5)); } public static double find(int n) { double l = 1; double r = n; double mid = (l + r) / 2.0; double p = 0.1; while (Math.abs(mid * mid - (double) n) > p) { if (mid * mid > n) { r = mid; } else if (mid * mid < n){ l = mid; } else { return mid; } mid = (l + r) / 2.0; } return mid; } }