题解 | #学英语#
学英语
https://www.nowcoder.com/practice/1364723563ab43c99f3d38b5abef83bc
解题思路
- 首先需要处理1-19的基本数字到英文的映射
- 处理20,30,40...90这样的整十数到英文的映射
- 根据数字的位数,将其分成不同的部分:
- 个位数和十位数(1-99)
- 百位数(hundred)
- 千位数(thousand)
- 百万位(million)
- 从高位到低位处理,按照规则拼接英文单词
- 注意处理and的添加规则:百位后面有数字时需要加and
代码
def numberToWords(num):
if num == 0:
return "zero"
ones = ["", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine",
"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen",
"seventeen", "eighteen", "nineteen"]
tens = ["", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"]
def helper(n):
if n == 0:
return ""
elif n < 20:
return ones[n] + " "
elif n < 100:
return tens[n//10] + " " + ones[n%10] + " "
elif n < 1000:
if n % 100 == 0:
return ones[n//100] + " hundred "
return ones[n//100] + " hundred and " + helper(n%100)
elif n < 1000000:
if n % 1000 == 0:
return helper(n//1000) + "thousand "
return helper(n//1000) + "thousand " + helper(n%1000)
else:
if n % 1000000 == 0:
return helper(n//1000000) + "million "
return helper(n//1000000) + "million " + helper(n%1000000)
ans = helper(num).strip()
return ans.replace(" ", " ")
# 处理输入
n = int(input())
print(numberToWords(n))
import java.util.*;
public class Main {
private final String[] ONES = {"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten",
"eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
private final String[] TENS = {"", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
public String numberToWords(int num) {
if (num == 0) return "zero";
return helper(num).trim();
}
private String helper(int num) {
StringBuilder result = new StringBuilder();
if (num >= 1000000) {
result.append(helper(num / 1000000)).append("million ");
num %= 1000000;
}
if (num >= 1000) {
result.append(helper(num / 1000)).append("thousand ");
num %= 1000;
}
if (num >= 100) {
result.append(ONES[num / 100]).append(" hundred ");
if (num % 100 != 0) {
result.append("and ");
}
num %= 100;
}
if (num >= 20) {
result.append(TENS[num / 10]).append(" ");
if (num % 10 != 0) {
result.append(ONES[num % 10]).append(" ");
}
} else if (num > 0) {
result.append(ONES[num]).append(" ");
}
return result.toString();
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
System.out.println(new Main().numberToWords(n));
}
}
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Solution {
private:
vector<string> ones = {"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten",
"eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
vector<string> tens = {"", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
string helper(int num) {
if (num == 0) return "";
string result;
if (num >= 1000000) {
result += helper(num / 1000000) + "million ";
num %= 1000000;
}
if (num >= 1000) {
result += helper(num / 1000) + "thousand ";
num %= 1000;
}
if (num >= 100) {
result += ones[num / 100] + " hundred ";
if (num % 100 != 0) {
result += "and ";
}
num %= 100;
}
if (num >= 20) {
result += tens[num / 10] + " ";
if (num % 10 != 0) {
result += ones[num % 10] + " ";
}
} else if (num > 0) {
result += ones[num] + " ";
}
return result;
}
public:
string numberToWords(int num) {
if (num == 0) return "zero";
string result = helper(num);
// 删除末尾空格
while (result.back() == ' ') {
result.pop_back();
}
return result;
}
};
int main() {
int n;
cin >> n;
Solution solution;
cout << solution.numberToWords(n) << endl;
return 0;
}
算法及复杂度
- 算法:字符串处理,递归
- 时间复杂度:
- 与输入数字的位数相关
- 空间复杂度:
- 递归调用栈的深度与数字的位数相关