✨ feat(solutions):新增欧拉问题17、18和67的解决方案 📝 docs(solutions/0018.MaxPathSum1):添加说明文档指出初始解法存在局部最优问题 ✨ feat(solutions/0067.MaxPathSum2):实现基于最大加代数的优化解法
72 lines
1.5 KiB
Python
72 lines
1.5 KiB
Python
"""
|
|
If the numbers 1 to 5 are written out in words: one, two, three, four, five,
|
|
then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.
|
|
|
|
If all the numbers from 1 to 1000 inclusive were written out in words,
|
|
how many letters would be used?
|
|
|
|
总之感觉这个问题很傻……没什么可说的。
|
|
"""
|
|
|
|
key: dict[int, str] = {
|
|
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",
|
|
}
|
|
|
|
|
|
def int_to_str(n: int) -> str:
|
|
if n > 1000:
|
|
return ""
|
|
elif n in key:
|
|
if n == 100:
|
|
return key[1] + key[n]
|
|
elif n == 1000:
|
|
return key[1] + key[1000]
|
|
else:
|
|
return key[n]
|
|
elif n < 100:
|
|
return key[n - n % 10] + key[n % 10]
|
|
elif n < 1000:
|
|
return (
|
|
key[n // 100]
|
|
+ key[100]
|
|
+ ("" if n % 100 == 0 else "and" + int_to_str(n % 100))
|
|
)
|
|
else:
|
|
return ""
|
|
|
|
|
|
def main() -> None:
|
|
print(sum(len(int_to_str(i)) for i in range(1, 1001)))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|