其他分享
首页 > 其他分享> > 牛客华为机试HJ107

牛客华为机试HJ107

作者:互联网

原题传送门

1. 题目描述

2. Solution

1、思路分析
二分法
2、代码实现

import sys

if sys.platform != "linux":
    sys.stdin = open("input/HJ107.txt")


def solve(n):
    if n >= 0:
        if n < 1:
            lo, hi = 0, 1
        else:
            lo, hi = 1, n
    else:
        if n > -1:
            lo, hi = -1, n
        else:
            lo, hi = n, -1
    tol = 0.001
    while hi - lo > tol:
        mid = (hi + lo) / 2
        mid_pow3 = mid * mid * mid
        if mid_pow3 < n:
            lo = mid
        elif mid_pow3 > n:
            hi = mid
        else:
            print(mid)
    print(round(lo, 1))


for line in sys.stdin:
    n = float(line.strip())
    solve(n)

标签:lo,mid,else,sys,牛客,hi,pow3,机试,HJ107
来源: https://www.cnblogs.com/junstat/p/16181417.html