题解 | #求解立方根#

求解立方根

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

const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;

void async function () {
    // Write your code here
    while(line = await readline()){
        const num = Number(line)
        const x = 0.0001;
        let low = Math.min(-1,num)
        let high = Math.max(1,num)
        let center = (low+high)/2
        while(Math.abs(center**3 - num) >= x){
            if(center**3<num){
                low = center
            }else{
                high = center
            }
            center = (low+high)/2
        }
        console.log(center.toFixed(1))
    }
}()

全部评论

相关推荐

HNU_fsq:建议直接出国,这简历太6了。自愧不如
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务