题解 | #字符串分隔#
字符串分隔
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 () {
while(line = await readline()){
const len = line.length;
if (len % 8 !== 0) {
/* n: 对字符串的长度除以8 向下取整再加1 可以得知终止条件值
如:字符串长度为22,算得n等于3,
3 * 8 == 24
24 - 22 == 2
于是需要在字符串末尾补充2个0
*/
const n = Math.floor(len / 8) + 1;
for (let i = len; i < 8 * n; i++) {
line += 0;
}
}
while (line.length > 0) {
// 每8位打印一次
console.log(line.slice(0,8));
// 对字符串重新赋值
line = line.slice(8);
}
}
}()
查看30道真题和解析
