Problem 17

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.
如果1到5写成英语,然后再把英语单词的字母数量加起来,我们会得到19。
If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?
如果所有的从1到1000(包括1000)的数字都写成英语单词,那需要多少个字母呢?
NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters and 115 (one hundred and fifteen)
contains 20 letters. The use of "and" when writing out numbers is in compliance with British usage.
注意:不要计算空白符以及连字符,需要计入‘and’单词。
def number_to_word(num: int) -> int:
determine_thousand = lambda num: int(str(num)[-4]) if len(str(num)) >= 4 else 0
thousand = determine_thousand(num)
determine_hundred = lambda num: int(str(num)[-3]) if len(str(num)) >= 3 else 0
hundred = determine_hundred(num)
determine_ten = lambda num: int(str(num)[-2]) if len(str(num)) >= 2 else 0
ten = determine_ten(num)
one = int(str(num)[-1]) word = 0
if ten == 1:
word += ten_to_twenty(int(str(num)[-2:]))
else:
word += one_digit(one)
word += ten_digit(ten)
word += hundred_digit(hundred)
if hundred:
if ten or one:
word += 3 # and
word += thousand_digit(thousand)
return word def one_digit(num: int) -> int:
if num == 0:
return 0
word = 0
if num in [1, 2, 6]: # one, two, six, ten
word = 3
elif num in [3, 7, 8]: # three, seven, eight
word = 5
else: # 4, 5, 9 four, five, nine
word = 4
return word def ten_digit(num: int) -> int:
if num == 0:
return 0
word = 0
if num in [2, 3, 8, 9]: # twenty, thirty, eighty, ninety
word = 6
elif num in [4, 5, 6]: # forty, fifty, sixty
word = 5
elif num == 7: # seventy
word = 7
return word def hundred_digit(num: int) -> int:
if num == 0:
return 0
word = 0
word = one_digit(num)
word += 7 # hundred
return word def thousand_digit(num: int) -> int:
if num == 0:
return 0
word = 0
word = one_digit(num)
word += 8 # thousand
return word def ten_to_twenty(num: int) -> int:
if num == 0:
return 0
word = 0
if num == 10: # ten
word = 3
elif num in [11, 12]: # eleven, twelve
word = 6
elif num in [13, 14, 18, 19]: # thirteen, fourteen, eighteen, nineteen
word = 8
elif num in [15, 16]: # fifteen, sixteen
word = 7
elif num == 17: # seventeen
word = 9
return word if __name__ == '__main__':
tot = 0
for i in range(1001):
word = number_to_word(i)
print(i, word)
tot += word
print(tot)

Problem 17的更多相关文章

  1. (Problem 17)Number letter counts

    If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + ...

  2. 【UOJ #17】【NOIP 2014】飞扬的小鸟

    http://uoj.ac/problem/17 dp,注意细节. #include<cstdio> #include<cstring> #include<algorit ...

  3. UOJ #17. 【NOIP2014】飞扬的小鸟 背包DP

    #17. [NOIP2014]飞扬的小鸟 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 4902  Solved: 1879 题目连接 http:// ...

  4. Common Bugs in C Programming

    There are some Common Bugs in C Programming. Most of the contents are directly from or modified from ...

  5. 江西理工大学南昌校区cool code竞赛

    这次比赛原本就是来打酱油的,想做个签到题就走!一开始不知道1002是签到题,一直死磕1001,WA了四发过了,回头一看Rank,三十名,我靠!看了1001的AC率,在我AC之前只有一个人AC了,当时我 ...

  6. CF17E:Palisection——题解

    https://vjudge.net/problem/CodeForces-17E http://codeforces.com/problemset/problem/17/E 题目大意:给一个长度为n ...

  7. LeetCode算法题目解答汇总(转自四火的唠叨)

    LeetCode算法题目解答汇总 本文转自<四火的唠叨> 只要不是特别忙或者特别不方便,最近一直保持着每天做几道算法题的规律,到后来随着难度的增加,每天做的题目越来越少.我的初衷就是练习, ...

  8. B. Hierarchy

    http://codeforces.com/problemset/problem/17/B 用邻接矩阵建图后, 设cost[v]表示去到顶点v的最小值. 很多个人去顶点v的话,就选最小的那个就OK 然 ...

  9. Python练习题 045:Project Euler 017:数字英文表达的字符数累加

    本题来自 Project Euler 第17题:https://projecteuler.net/problem=17 ''' Project Euler 17: Number letter coun ...

随机推荐

  1. FreeRTOS系列第20篇---FreeRTOS信号量API函数

    FreeRTOS的信号量包括二进制信号量.计数信号量.相互排斥信号量(以后简称相互排斥量)和递归相互排斥信号量(以后简称递归相互排斥量).我们能够把相互排斥量和递归相互排斥量看成特殊的信号量. 信号量 ...

  2. 南昌互联网行业协会筹办者祝真和华罡团队-2014年12月江西IDC排行榜

     他出自军营,拥有一身正气. 他在南昌创业,立意卓越. 从站点開始.到微营销.到线上教育,全面开花. 他在朋友圈看到不对的内容,就会即时批评. 他对朋友,又是很的和蔼可亲. 他就是南昌华罡网络创办 ...

  3. hihoCoder - 1079 - 离散化 (线段树 + 离散化)

    #1079 : 离散化 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描写叙述 小Hi和小Ho在回国之后,又一次过起了朝7晚5的学生生活.当然了.他们还是在一直学习着各种算法 ...

  4. spring+springmvc+hibernate架构、maven分模块开发样例小项目案例

    maven分模块开发样例小项目案例 spring+springmvc+hibernate架构 以用户管理做測试,分dao,sevices,web层,分模块开发測试!因时间关系.仅仅測查询成功.其它的准 ...

  5. Xcode 自己主动生成版本技术最佳实践

    在 bloglovin ,我们使用自己主动生成版本来设置Xcode,使当前的版本为在Git活跃的分支上 的提交数. 它一直正常工作着.但我们的技术也不是一帆风顺的. 糟糕的老方法 我们使用的技术是来自 ...

  6. CPPCMS库在Windows下的使用

    标题:CPPCMS库在Windows下的使用时间:2012-7作者:Kagula 环境:[1]WinXP SP3[2]VisualStudio2008 SP1[3]ZLib 1.2.7[4]PCRE ...

  7. Sorting It All Out(拓扑排序)

    http://poj.org/problem?id=1094 1.判断所给关系是否为合法的拓扑序列,若存在环,输出 "Inconsistency found after %d relatio ...

  8. 【K8s】Kubernetes架构理解

    抽空学习了一下Kubernetes,感觉和大数据领域内集群的资源管理.任务调度等有异曲同工之处,简单总结一下备忘. [概念] Kubernetes是一个工业级的容器编排平台,单词有点长,常用K8s代称 ...

  9. Cracking the Coding Interview 8.5

    Implement an algorithm to print all valid combinations of n-pairs of parentheses #include<stdio.h ...

  10. java反射机制学习小结

    之前一直对java的反射机制理解得很模糊,今天因为学习spring,所以花了些时间总算把它理顺了,记录一下 另外,推荐读读这篇文章,写的挺好的http://blog.csdn.net/woshixuy ...