题解 | #字符串分隔#

字符串分隔

https://www.nowcoder.com/practice/d9162298cb5a437aad722fccccaae8a7

const readline = require("readline");

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

const splitLength = 8;

const getStr = (str: string) => {
    const targetIndex =
       str.length % splitLength === 0
            ? str.length / splitLength - 1
            : Math.floor(str.length / splitLength);
    for (let i = 0; i <= targetIndex; i++) {
        const subStr = str.substring(i * splitLength, (i + 1) * splitLength);
        const targetStr = subStr?.length ? subStr.padEnd(splitLength, "0") : "";
        console.log(targetStr);
    }
};

rl.on("line", function (line) {
    getStr(line);
});

全部评论

相关推荐

喜欢疯狂星期四的猫头鹰在研究求职打法:短作业优先
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务