题解 | #求解立方根#

求解立方根

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

import math

from decimal import Decimal


def main():
    a = input()
    # print(a)
    a = float(a)

    if abs(a) <= 20:
        if a >= 0:
            b = math.pow(a, 1 / 3)
            b = Decimal(b).quantize(Decimal("0.1"), rounding="ROUND_HALF_UP")
        else:
            b = math.pow(-a, 1 / 3)
            b = -Decimal(b).quantize(Decimal("0.1"), rounding="ROUND_HALF_UP")

    print(b)

    # if abs(a) <= 20:
    #     if a >= 0:
    #         b = 0
    #         while b < 2.715:
    #             c = b * b * b
    #             if c >= a:
    #                 print(Decimal(b).quantize(Decimal("0.1"), rounding="ROUND_HALF_UP"))
    #                 break
    #             b += 0.01
    #     else:
    #         b = 0
    #         while b > -2.715:
    #             c = b * b * b
    #             if c <= a:
    #                 print(Decimal(b).quantize(Decimal("0.1"), rounding="ROUND_HALF_UP"))
    #                 break
    #             b -= 0.01


main()

全部评论

相关推荐

专心打鱼:互联网搬运工,贴子都要偷
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务