题解 | #求小球落地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) {
    let totalLength = 0
    function setTotalLength(x){
        totalLength = x
    }
    let fantan = 0
    function setFantan(x){
        fantan = x
    }
    const h = parseInt(line)
    solution(0,h,'top',0,h,setTotalLength,setFantan)
    console.log(totalLength)
    console.log(fantan)
    
});

function solution(time,h,location,accLength,lastH,setTotalLength,setFantan){
    if(time === 5 && location === "top"){
        setFantan(h)
        return
    }
    if(time === 5 &&  location === "down"){
        setTotalLength(accLength)
    }
    if(location === "top"){
        // 落地
        return solution(time+1,0,'down',accLength+h,h,setTotalLength,setFantan)
    }
    if( location === 'down'){
        // 反弹
        const newH = lastH/2
        return solution(time,newH,'top' ,accLength+newH,newH,setTotalLength,setFantan)
    }
}

全部评论

相关推荐

过往烟沉:我说什么来着,java就业面就是广!
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务