题解 | #小招喵跑步#

小招喵跑步

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]);
    }
}()

全部评论

相关推荐

感觉初筛都过不去,但是没挂我,我就先等着吧
投递华为技术有限公司等公司10个岗位
点赞 评论 收藏
分享
牛客83265014...:完了,连现在都没开始面,13号投的是不是晚了
秋招的第一个offer,...
点赞 评论 收藏
分享
今年hc多吗。。双非真无力了
投递比亚迪等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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