题解 | #字符串分隔#

字符串分隔

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

// JavaScript Node
const readline = require('readline')
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
})
function subChar(args){
    const arr = []
    args.forEach((item,index)=>{
        if(item.length % 8 === 0 && item !== ''){
            for(let i=0;i<item.length;i = i+8){
                const newC = item.substring(i,i+8)
                arr.push(newC)
            }
        }else{
            if(item.length / 8 > 1){
                for(let i=0;i<item.length; i=i+8){
                    if(item.length - i < 8){
                        item = item.substring(i)
                        while(item.length<8){
                            item = item + 0
                        }
                        arr.push(item)
                    }else{
                        const newC = item.substring(i,i+8)
                        arr.push(newC)
                    }
                }
            }else{
                while(item.length<8){
                    item = item + 0
                }
                arr.push(item)
            }
        }
    })
    for(const item of arr){
        console.log(item)
    }
}

rl.on('line', function(line){
    const tokens = line.split(' ')
    subChar(tokens)
})

全部评论

相关推荐

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