题解 | #学英语#

学英语

https://www.nowcoder.com/practice/1364723563ab43c99f3d38b5abef83bc

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 objMap = {
        1: "one",
        2: "two",
        3: "three",
        4: "four",
        5: "five",
        6: "six",
        7: "seven",
        8: "eight",
        9: "nine",
        10: "ten",
        11: "eleven",
        12: "twelve",
        13: "thirteen",
        14: "fourteen",
        15: "fifteen",
        16: "sixteen",
        17: "seventeen",
        18: "eighteen",
        19: "nineteen",
        20: "twenty",
        30: "thirty",
        40: "forty",
        50: "fifty",
        60: "sixty",
        70: "seventy",
        80: "eighty",
        90: "ninety",
        100: "hundred",
        1000: "thousand",
        1000000: "million",
        100000000: "billion",
    };
    const objArr = Object.keys(objMap).reverse();
    // 拆解数值 获取拆解的数组
    function getNumsArr(num) {
        const resArr = [];
        while (num > 0) {
            objArr.some((dividend) => {
                dividend = +dividend;
                const innerRes = num / dividend;
                const innerRemainder = num % dividend;
                // 大于1 代表可以分解 从大到小进行除法与取余
                if (innerRes >= 1) {
                    const parseIntInnerRes = parseInt(innerRes);
                    resArr.push({
                        // 余数
                        remainder: innerRemainder,
                        // 除结果
                        res: parseIntInnerRes,
                        // 对应的单位
                        dividend,
                        children:
                        // 对于大于100的需要继续分解
                            parseIntInnerRes > 100
                                ? getNumsArr(parseIntInnerRes)
                                : [],
                    });
                    // 赋值 达到循环的条件
                    num = innerRemainder;
                    // 利用some的特点,减少循环
                    return true;
                }
            });
        }
        return resArr;
    }
    // 根据拆解的数组格式化展示值
    function formaterRes(res) {
        return res
            .map((item, index) => {
                const haveChildren = item.children.length > 0;
                if (haveChildren) {
                    // 大于100的这里还需要加上单位dividend  four hundred and eighty six (针对这里) thousand six hundred and sixty nine
                    return `${formaterRes(item.children)} ${
                        objMap[item.dividend]
                    }`;
                } else {
                    // 如果是 50 9 这种不用额外加1 这种情况one fifty
                    if (
                        item.dividend >= 1 &&
                        item.dividend < 100 &&
                        item.res === 1
                    ) {
                        return `${objMap[item.dividend]}`;
                    } else {
                        return `${objMap[item.res]} ${objMap[item.dividend]}${
                            item.dividend === 100 && res.length - 1 !== index
                                ? " and"
                                : ""
                        }`;
                    }
                }
            })
            .join(" ");
    }

    while ((line = await readline())) {
        let num = +line;
        console.log(formaterRes(getNumsArr(num)));
    }
})();

#题解#
全部评论

相关推荐

威猛的小饼干正在背八股:挂到根本不想整理
点赞 评论 收藏
分享
10-21 23:48
蚌埠坦克学院
csgq:可能没hc了 昨天一面完秒挂
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务