题解 | 字符串分隔

字符串分隔

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

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
    while(line = await readline()){
        // 得到缺少的字符数。
        let theMod = 8 - (line.length % 8)
        if(theMod === 8){
            theMod = 0
        }
        // 补足缺少的字符数。
        let fillLine = `${line}${new Array(theMod).fill('0').join('')}`
        let theString = ''
        for(let index = 0;index<fillLine.length;index++){
            theString = `${theString}${fillLine[index]}`
            // 每8个字符遍历一次。
            if(theString.length===8){
                console.log(theString)
                theString = ''
            }
        }
    }
}()

补足字符数,之后遍历并拼接子字符串,够8次之后打印。

全部评论

相关推荐

03-19 09:58
河海大学 Java
最喜欢春天的奇亚籽很...:同学,是小红书不是小哄书,一眼就能看到的错误
投了多少份简历才上岸
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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