题解 | #小招喵跑步#

小招喵跑步

https://www.nowcoder.com/practice/1177e9bd1b5e4e00bd39ca4ea9e4e216?tpId=182&tqId=34573&rp=1&ru=/exam/company&qru=/exam/company&sourceUrl=%2Fexam%2Fcompany&difficulty=undefined&judgeStatus=undefined&tags=&title=

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
    line = await readline();
    x = parseInt(line);
    
    if(x<2&&x>=0){
        console.log(x);
    }else{
    if(x < 0){
        x = -x;
    }
    let dp = [];
    dp[0]=0;
    dp[1]=1;
    for(let i=2; i<=x; i++){
        if(i%2==0){
            dp[i] = dp[i/2]+1;
        }else{
            dp[i] = Math.min(dp[i-1], 1+dp[(i+1)/2])+1
        }
    }
    console.log(dp[x]);
    }
}()

全部评论

相关推荐

点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务