题解 | #求解立方根#
求解立方根
http://www.nowcoder.com/practice/caf35ae421194a1090c22fe223357dca
import sys while True: try: n = float(input()) sig = 1 #考虑小于0的数,转成正数处理 if n < 0: sig = -1 n = -n #使用二分法查找 #考虑小于1的数,起点是n,终点是1 #大于等于1的数,起点是0,终点是n if n < 1: start = n end = 1 else: start = 0 end = n g = (start + end) / 2 while abs(g**3 - n) > 0.01: # print(g) if g**3 > n: end = g else: start = g g = (start + end) / 2 print(round(sig*g,1)) except: # print(sys.exc_info()) break