题解 | #学英语#
学英语
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 () {
const n = Number(await readline());
const word1 =
"zero one two three four five six seven eight nine ten eleven twelve thirteen forteen fifteen sixteen seventeen eighteen nineteen".split(
" "
);
const word2 =
"0 0 twenty thirty forty fifty sixty seventy eighty ninety".split(" ");
let word = [];
// 100以内转英文
function n2w(n) {
if (n > 0) {
if (n < 20) {
word.push(word1[n]);
} else {
word.push(word2[Math.floor(n / 10)]);
if (n % 10 !== 0) {
word.push(word1[n % 10]);
}
}
}
}
// 1000以内转英文
function hun(n) {
if (n >= 100) {
word.push(word1[Math.floor(n / 100)]);
word.push("hundred");
if (n % 100 !== 0) {
word.push("and");
}
}
n2w(n % 100);
}
let a = n % 1000, // 个十百位
b = Math.floor(n / 1000) % 1000, // 个十百千
c = Math.floor(n / 1000000) % 1000, // 个十百m
d = Math.floor(n / 1000000000); // 个十百b
if (d > 0) {
hun(d);
word.push("billion");
}
if (c) {
hun(c);
word.push("million");
}
if (b) {
hun(b);
word.push("thousand");
}
if (a) {
hun(a);
}
console.log(word.join(" "));
})();
查看3道真题和解析