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. DM8168 IPNC Boa移植

    1.交叉编译openssL 下载openssL-1.0.0.tar.gz在虚拟机下进行交叉编译,生成libcrypto.a及libssl.a.将这两个文件复制到DVRRDK_03.00.00.00/b ...

  2. 《黑马程序猿》 cocos2d游戏引擎复习笔记一

    /** ----------------------------游戏场景的搭建-------------------------------- 1首先创建一个surfaceview ,它能够在子线程中 ...

  3. Android 组件ContentProvider

    Android 组件ContentProvider Android的数据存储有五种方式Shared Preferences.网络存储.文件存储.外储存储.SQLite,一般这些存储都仅仅是在单独的一个 ...

  4. iOS:在UITextField中加入图标

    //最左側加图片是下面代码 右側相似 UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@&q ...

  5. 牛逼的postman,分类管理更好用

  6. 截取字符(substr)检索字符位置(instr)

    1.SUBSTR(string,start_position,[length]) 求子字符串,返回字符串注释: string 元字符串start_position 开始位置(从0开始)length 可 ...

  7. Java同步容器总结

    <0>StringBuffer适用于多线程场景,StringBuilder适用于字符串拼接[堆栈封闭] `Vector`实现`List`接口,底层和`ArrayList`类似,但是`Vec ...

  8. Mvc程序字体加载失败问题

    在我们开发的asp.net-mvc项目中,有时会出现字体加载失败的现象,但是一检查字体文件目录,发现文件目录都是存在的且有效的,这是为何呢?原来需要再web.config文件中添价少许配置代码就搞定. ...

  9. JQuery+Bootstrap总结

    ================JQuery=========== JQuery 1. jQuery是什么? 一个js插件, 相比较原生的DOM操作更简单.开发效率更高 2. jQuery使用 1. ...

  10. Spring Boot (2) Restful风格接口

    Rest接口 动态页面jsp早已过时,现在流行的是vuejs.angularjs.react等前端框架 调用 rest接口(json格式),如果是单台服务器,用动态还是静态页面可能没什么大区别,如果服 ...