题解 | #字符串分隔#

字符串分隔

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
    let token = [];
    while(line = await readline()){
        let tokens = line.split(' ');
        token =[...tokens[0]]//用扩展符将保存在tokens[0]中的字符串,每一项分别保存在token中
    }

    for(let i = token.length%8; 0 < i&&i < 8; i++)//for循环中判断条件用&&或者||链接条件,可以无限链接
    {
        token.push("0")
    }

    for(let i = 0; i < token.length; i = i + 8){
        console.log(token.slice(i,i+8).join(''))//slice不包括后面这一项
    }

}()

我的方法

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
    let inputString = ''//直接用空字符串接收输入的line
    while(line = await readline()){
        inputString = line.split('');
    }
    let finalArr = []
    while(inputString.length % 8 !=0){//比我的方法好,判断条件少,不容易出现加8个0的错误
        inputString.push('0')
    }
    for(let i = 0;i < inputString.length ;i = i + 8){
        finalArr.push(inputString.slice(i,i+8));
    }
    finalArr.forEach((item) => {
        console.log(item.join(''))
    })
}()

满洲里韦布——的方法

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 str = line + '00000000'; // 先补0,这样最后长度肯定大于8
        for (let i = 8; i < str.length; i += 8) { // i从8开始 保证substring从0开始
            console.log(str.substring(i - 8, i)); 
        }
    }
}()

牛客553602196号——的方法

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 arr = line.split('')
        let addNum = arr.length % 8 == 0 ? 0 : (8 - arr.length % 8)//三目运算符
        for(let i = 0; i < addNum; i++){
            arr.push('0')
        }
        let str = ''
        for(let i = 1; i <= arr.length; i++){
            str += arr[i - 1]
            if(i % 8 === 0){
                console.log(str)
                str = ''
            }
        }
    }
}()

牛客944083017号——的方法

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 start = 0;
        let endIndex = start + 7;
        let length = line.length;
        let token;
        while (endIndex < length) {
            //get token
            token = line.slice(start, endIndex + 1);
            //update index
            start = endIndex + 1;
            endIndex = start + 7;
            console.log(token);
        }
        //when endIndex > length
        if (start !== length) {
            //get the token
            token = line.slice(start, length);
            for (let i = length; i <= endIndex; i++) {
                token = token.concat("0");//concat() 方法用于连接两个或多个字符串。
            }
            console.log(token);
        }
    }
})();

Jessiiiiie——的方法

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 len = line.length;
        for (let i = 0; i < len; i = i + 8) {
            let substr = line.substring(i, i + 8);
            if (substr.length === 8) {
                console.log(substr);
            } else {
                console.log(substr.padEnd(8, '0'));//返回新的字符串,表示用参数字符串从尾部(右侧)补全原字符串。
            }
        }
    }
}()

牛客231636961号——的方法

const readline = require("readline");
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});
rl.on("line", function (lineInput) {
  const line = lineInput.split(" ").join("");
    for (let i = 0; i <= line.length; i++) {
      if (i % 8 === 0) {
        let t = line.slice(i, i + 8);
        if  ( 0 < t.length  && t.length < 8) {
          t = t + "0".repeat(8 - t.length);//返回新的字符串,表示将字符串重复指定次数返回。
        }
        console.log(t);
      }
    }
});

多崎造有颜色——的方法

const readline = require("readline");

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});
let lines = "";
rl.on("line", function (line) {
  lines = line;
  let len = lines.length;
  if (len <= 8) {
    console.log(lines.padEnd(8, "0"));
  }else{
      let k =  Math.floor(len/8)//floor() 方法返回小于等于x的最大整数。如果传递的参数是一个整数,该值不变。
      for(let i = 0; i<k; i++){
          console.log(lines.slice(i*8,(i+1)*8))
      }
      if(k*8 < len)
      console.log(lines.slice(k*8).padEnd(8, '0'))
  }
});

最前端——的方法

练练练练练 文章被收录于专栏

练练练练练

全部评论

相关推荐

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