题解 | #人民币转换#
人民币转换
https://www.nowcoder.com/practice/00ffd656b9604d1998e966d555005a4b
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 words = ["零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"]; const eachWan = (num) => { let str = ""; let key = 0; while (num > 0) { if (key === 0) { if (num % 10 > 0) str = words[num % 10]; } else if (key === 1) { if (num % 10 === 1) str = "拾".concat(str); else if (num % 10 > 0) str = words[num % 10].concat("拾", str); else str = "零".concat(str); } else if (key === 2) { if (num % 10 > 0) str = words[num % 10].concat("佰", str); else if (str[0] !== "零") str = "零".concat(str); } else if (key === 3) { if (num % 10 > 0) str = words[num % 10].concat("仟", str); else if (str[0] !== "零") str = "零".concat(str); } num = parseInt(num / 10); key++; } return str; }; while ((line = await readline())) { let tokens = line.split(".").map(Number); let str = ""; if (tokens.length === 1) str = str.concat("整"); else { let str1 = ""; let str2 = ""; if (parseInt(tokens[1]) === 0) { str2 = "整"; } else if (parseInt(tokens[1] / 10) > 0) { str1 = words[parseInt(tokens[1] / 10)].concat("角"); if (tokens[1] % 10 !== 0) str2 = words[tokens[1] % 10].concat("分"); } else str2 = words[tokens[1]].concat("分"); str = str1.concat(str2, str); } let intNumber = tokens[0]; let key = 0; let qianStr = ""; while (intNumber > 0) { if (key === 0) { qianStr = eachWan(intNumber % 10000).concat("元"); key++; } else if (key === 1) { qianStr = eachWan(intNumber % 10000).concat("万", qianStr); key = 2; } else if (key === 2) { qianStr = eachWan(intNumber % 10000).concat("亿", qianStr); key = 1; } intNumber = parseInt(intNumber / 10000); } console.log("人民币".concat(qianStr, str)); } })();