题解 | #求解立方根#

求解立方根

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))
    
    




全部评论

相关推荐

投递网易雷火等公司6个岗位
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务