计算一个浮点数的立方根,不使用库函数。
保留一位小数。
数据范围:
a = float(input()) positive = a >= 0 a *= (positive*2-1) b = a**(1/3) print(round((positive*2-1)*b, 1))
n = float(input()) if n == 0: print('0.0') elif n>0: ans = str(n**(1/3)).split('.') if int(ans[1][1])<5: print(f'{ans[0]}.{ans[1][0]}') else: print(f'{ans[0]}.{int(ans[1][0])+1}') else: n *= -1 ans = str(n**(1/3)).split('.') if int(ans[1][1])<5: print(f'-{ans[0]}.{ans[1][0]}') else: print(f'-{ans[0]}.{int(ans[1][0])+1}')
while True: try: a=float(input()) #泰勒公式 x(n+1)=x(n)-△x=x(n)-f(x(n))/f'(x(n)) t=a while abs(t**3-a)>0.00001: t=((a-t**3)/(3.0*t**2))+t print(round(t,1)) #二分法 m,n=-20,20 mid=(m+n)/2 while abs(mid**3-a)>0.0001: if mid**3<a: m=mid else: n=mid mid=(m+n)/2 #print(round(mid,1)) except: break
def func(n): if n == 0: return 0 if abs(n) == 1 : return n if n>0: left = 0 right = n if n > 1 else 1 else: left = n if n < -1 else -1 right = 0 while True: mid = (left + right) / 2 t = mid * mid * mid if abs(t-n)<0.01: return round(mid*10)/10 elif t > n: right = mid elif t < n: left = mid x = float(input()) print("%.1f" % (func(x)))
while True: try: a = input() if a[0]=='-': a = a[1::] b = float(a)**(1/3) print('-'+'%.1f'%b) else: b = float(a)**(1/3) print('%.1f'%b) except: break
num=int(float(input())*1000) cc=0 if num<0: cc=1 num=num*-1 c=[] c1=[] b=[] b1=[] for i in range (0,30): a=num-i*i*i if a>=0: c.append(a) c1.append(i) else: b.append(a) b1.append(i) if cc==0: if c[len(c)-1]>b[0]*-1: print(len(c)/10) else: print((len(c)-1)/10) else: if c[len(c)-1]>b[0]*-1: print(-len(c)/10) else: print(-(len(c)-1)/10)
while True: try: a = float(input().strip()) epsilon = 0.0001 low = min(-1.0, a) #左边界 high = max(1.0, a) #右边界 ans = (low + high)/2 while abs(ans**3 - a) >= epsilon: if ans**3 < a:# ans小了 low = ans #要变大,所以移动左边界,使下一次ans变大 else: # ans大了 high = ans #要变小。所以移动右边界,使下一次ans变小 ans = (low + high)/2.0 print(round(ans,1)) #输出参数的立方根。保留一位小数 except: break