题解 | #求解立方根#
求解立方根
http://www.nowcoder.com/practice/caf35ae421194a1090c22fe223357dca
逐位探索,直到最接近求解值。
while True:
try:
Num = float(input())
if Num<0:
N_flag = -1
Num = (-1)*Num
elif Num == 0:
result = 0
else:
N_flag = 1
i = 1
while(i**3<=Num):
i+=1
j=0
while((i-1)+j*0.1)**3 <= Num:
j+=1
k=0 #为了对小数点后第二位进行四舍五入,需要计算到第二位。
while((i-1)+(j-1)*0.1+k*0.01)**3 <= Num:
k+=1
if k>=5: #四舍五入
result =(i-1 + (j)*0.1) * N_flag #注意处理负数
else:
result =(i-1 + (j-1)*0.1) * N_flag
#print(result)
print(round(result,1)) ##最坑莫过于此,要取round
except:
break