题解 | 字符串分隔
字符串分隔
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次之后打印。
查看9道真题和解析