题解 | #求解立方根#
求解立方根
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()