题解 | 字符串分隔

字符串分隔

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-11 23:33
已编辑
曲阜师范大学 后端工程师
牛客68808588...:果真开发过12306购票系统吗,这不是一眼就被看穿了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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