题解 | #求解立方根#

求解立方根

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

import sys


def x3(s_input): 

    if s_input == 0:
        return 0
    elif s_input < 0:
        left = -20
        right = 0
        x = -2
        s1 = -8
        while abs(s1 - s_input) > 0.01:
            if s1 < s_input:
                left = x
            elif s1 > s_input:
                right = x    
            else:
                return x
            x = right - (right - left)/2
            s1 = x*x*x
        return x
    else:
        left = 0
        right = 20
        x = 2
        s1 = 8
        while abs(s1 - s_input) > 0.01:
            if s1 < s_input:
                left = x
            elif s1 > s_input:
                right = x    
            else:
                return x
            x = left + (right - left)/2
            s1 = x*x*x
        return x

for line in sys.stdin:
    a = line.split()
    s_input = float(a[0])
    x1 = x3(s_input)
    print("{:.1f}".format(x1))
    
    




全部评论

相关推荐

菠落蜜:这个是系统自动投的,不是hr主动打招呼。更抽象的还有ai回复
我的秋招日记
点赞 评论 收藏
分享
做个有文化的流氓:幸遇良师,幸遇好的hr
找工作中的小确幸
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务