题解 | #求解立方根#
求解立方根
http://www.nowcoder.com/practice/caf35ae421194a1090c22fe223357dca
一开始low和high的取值考虑到a比设为(-1,1)后面分类和a比较要更简洁
a = float(input())
low = min(-1, a)
high = max(1,a)
ans = (low + high) / 2
e = 0.00001
while abs(ans ** 3 - a) > e: #只有两个情况,小了右移,大了左移,更新low,high
if ans**3 < a:
low = ans
else:
high = ans
ans = (high + low) / 2 #更新ans
print(format(ans,'.1f'))