题解 | #求解立方根#

求解立方根

https://www.nowcoder.com/practice/caf35ae421194a1090c22fe223357dca

while True:
    try:
        number = float(input())
        first = min(-1., number)
        last = max(1., number)
        precison = 0.00001
        ans = (first + last) / 2
        while abs(ans**3 - number) > precison:
            if ans ** 3 < number:
                first = ans
            else:
                last = ans
            ans = (first + last) / 2.0
        print("%.1f" %ans)
    except:
        break

二分法实现估计,注意打印格式的设置

全部评论

相关推荐

2024-12-30 22:31
吉首大学 Web前端
工字钢写代码:改成吉林就OK了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务