标签:else code fun double 二分 bsp 面试 ble 面试题
面试题:不使用库函数求一个数的立方根。
解法:二分法。
1 double fun(double x) { 2 if (x == 0) return 0; 3 double low = 0; 4 double top = x; 5 if (x < 0) { 6 low = x; 7 top = 0; 8 } 9 while (low<top) 10 { 11 double mid = (low + top) / 2; 12 double tmp = mid*mid*mid; 13 if (tmp > x) { 14 top = mid; 15 } 16 else if (tmp < x) { 17 low = mid; 18 } 19 else { 20 return mid; 21 } 22 } 23 }
标签:else code fun double 二分 bsp 面试 ble 面试题
原文地址:https://www.cnblogs.com/yuchi328/p/9688056.html