Problem 17
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的更多相关文章
- (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 + ...
- 【UOJ #17】【NOIP 2014】飞扬的小鸟
http://uoj.ac/problem/17 dp,注意细节. #include<cstdio> #include<cstring> #include<algorit ...
- UOJ #17. 【NOIP2014】飞扬的小鸟 背包DP
#17. [NOIP2014]飞扬的小鸟 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 4902 Solved: 1879 题目连接 http:// ...
- Common Bugs in C Programming
There are some Common Bugs in C Programming. Most of the contents are directly from or modified from ...
- 江西理工大学南昌校区cool code竞赛
这次比赛原本就是来打酱油的,想做个签到题就走!一开始不知道1002是签到题,一直死磕1001,WA了四发过了,回头一看Rank,三十名,我靠!看了1001的AC率,在我AC之前只有一个人AC了,当时我 ...
- CF17E:Palisection——题解
https://vjudge.net/problem/CodeForces-17E http://codeforces.com/problemset/problem/17/E 题目大意:给一个长度为n ...
- LeetCode算法题目解答汇总(转自四火的唠叨)
LeetCode算法题目解答汇总 本文转自<四火的唠叨> 只要不是特别忙或者特别不方便,最近一直保持着每天做几道算法题的规律,到后来随着难度的增加,每天做的题目越来越少.我的初衷就是练习, ...
- B. Hierarchy
http://codeforces.com/problemset/problem/17/B 用邻接矩阵建图后, 设cost[v]表示去到顶点v的最小值. 很多个人去顶点v的话,就选最小的那个就OK 然 ...
- Python练习题 045:Project Euler 017:数字英文表达的字符数累加
本题来自 Project Euler 第17题:https://projecteuler.net/problem=17 ''' Project Euler 17: Number letter coun ...
随机推荐
- Android 应用启动动画代码
requestWindowFeature(Window.FEATURE_NO_TITLE);//设置无标题 setContentView(R.layout.activity_main); getWin ...
- Unable to instantiate Action, xxxAction, defined for 'xxx' in namespace '/'xxxAction解决方式
出现这个问题的解决办法主要有两个 1.假设项目没有使用Spring,则struts.xml配置文件里,这个action的class属性的路径没有写完整,应该是包名.类名 2.假设项目使用了Spring ...
- 君正Ingenic X1000E_halley2 更改Logo
有两种方法可以改变开机logo,编译进内核或者修改u-boot. <一>.编译进内核 一. 制作LOGO图片(可以使用gimp) 1. 制作一个.ppm格式图片(logo_tvu_clut ...
- oc41--SEL
// // main.m // 代理的时候用. /* SEL类型: 代表方法的签名,在类中的方法列表中存储着该签名与该方法对应的关系,类的方法列表存储在类对象中, 每个方法都有一个与之对应的sel类型 ...
- oc26--Property,省略setget的声明
// // Person.h #import <Foundation/Foundation.h> @interface Person : NSObject { int _age; } /* ...
- curses-键盘编码-openssl加解密【转】
本文转载自;https://zhuanlan.zhihu.com/p/26164115 1.1 键盘编码 按键过程:当用户按下某个键时, 1.键盘会检测到这个动作,并通过键盘控制器把扫描码(scan ...
- expectation-maximization algorithm ---- PRML读书笔记
An elegant and powerful method for finding maximum likelihood solutions for models with latent varia ...
- Google Chrome调试常用快捷键
Ctrl+Shift+I调出开发者工具 Ctrl+R刷新(界面显示不清楚,刷新后变清楚)
- A. Jeff and Digits(cf)
A. Jeff and Digits time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- weui&flexible布局
1.weui 一开始以为只能用于小程序中,原来分两种:weui-wxss-master和weui-master.真的是强大的不得了,把设计好的样式和功能封装.然后分类,有明确的层级和逻辑,感动!!值得 ...