python 3 解法 ,考虑小于1以及负数的情况
求解立方根
http://www.nowcoder.com/questionTerminal/caf35ae421194a1090c22fe223357dca
import sys
def cube():
num = float(sys.stdin.readline().strip())
if num == 0:
return 0
if num > 0:
sig = 1
else:
sig = -1
num = abs(num)
if num > 1:
start = 0
end = num
else:
start = num
end = 1
mid = (end + start) / 2
while abs(mid ** 3 - num) > 0.001:
if mid ** 3 > num:
end = mid
else:
start = mid
mid = (end + start) / 2
print(round(sig * mid, 1))
cube()
查看14道真题和解析