题解 | #求解立方根#

求解立方根

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;
let curValue = 0,
    result = 0;
function getValue(num) {
    if (curValue ** 3 < num) {
        // 精度不够,就继续往下调整
        curValue += 0.01;
        // 记得要return一下,不然就没有返回值了
        return getValue(num);
    } else {
        return curValue;
    }
}
void (async function () {
    // Write your code here
    while ((line = await readline())) {
        // 这里保存一下正负号,后面就靠直接拼接了
        let isPossive = Number(line) >= 0 ? "" : "-";
        let num=Math.abs(Number(line));
        result = isPossive + getValue(num).toFixed(1);
        console.log(result);
    }
})();

全部评论

相关推荐

07-22 11:53
门头沟学院 Java
终于有一个保底的offer了,但感觉是白菜价
北凝a:我想问问,提前批的offer 有问你啥时候到岗吗,如果你还想找其他的怎么办
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务