题解 | #求小球落地5次后所经历的路程和第5次反弹的高度#

求小球落地5次后所经历的路程和第5次反弹的高度

https://www.nowcoder.com/practice/2f6f9339d151410583459847ecc98446

const readline = require("readline");

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
});

rl.on("line", function (line: string) {
    const initHeight = parseInt(line);
    console.log(getTotalDistance(initHeight, 5));
    console.log(getHeight(initHeight, 5));
});

const getTotalDistance = (initHeight: number, times: number): number => {
    let total = initHeight;
    if (times > 1) {
        for (let i = 0; i < times - 1; i++) {
            total += initHeight * Math.pow(0.5, i);
        }
    }
    return total;
};

const getHeight = (initHeight: number, times: number): number => {
    return Math.pow(0.5, times) * initHeight;
};

全部评论

相关推荐

03-29 14:19
门头沟学院 Java
你背过凌晨4点的八股文么:加油同学,人生的容错率很高,只是一个暑期罢了,后面还有很多机会!
点赞 评论 收藏
分享
03-11 14:28
浙江大学 设计
牛客小黄鱼:代入一下,独居女生会觉得有点可怕
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务