题解 | #字符串合并处理#

字符串合并处理

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

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
    const transfer = (str) => {
        let newStr = "0000";
        for (let i of str) {
            newStr = i.concat(newStr);
        }
        return newStr.slice(0, 4);
    };

    while ((line = await readline())) {
        let tokens = line.split(" ");
        let str = tokens.join("");
        let jiArr = [];
        let ouArr = [];
        let newArr = [];
        let newStr = "";
        for (let i = 0; i < str.length; i++) {
            i % 2 === 0 ? jiArr.push(str[i]) : ouArr.push(str[i]);
        }
        jiArr.sort();
        ouArr.sort();
        for (let i = 0; i < jiArr.length; i++) {
            newArr.push(jiArr[i]);
            if (ouArr[i]) newArr.push(ouArr[i]);
        }
        for (let i of newArr) {
            let er;
            if (/[0-9]/.test(i)) er = parseInt(i).toString(2);
            else if (/[a-fA-F]/.test(i)) er = parseInt(i, 16).toString(2);
            else {
                newStr = newStr.concat(i);
            }
            if (er) {
                let transNum = transfer(er);
                let final = parseInt(transNum, 2).toString(10);
                if (final > 9)
                    final = parseInt(final).toString(16).toUpperCase();
                newStr = newStr.concat(final);
            }
        }
        console.log(newStr);
    }
})();

全部评论

相关推荐

11-27 12:43
已编辑
门头沟学院 C++
点赞 评论 收藏
分享
shtdbb_:还不错,没有让你做了笔试再挂你
点赞 评论 收藏
分享
冲芭芭拉鸭:你这图还挺新,偷了。
投递美团等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务