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 ...
随机推荐
- CSS3:box-sizing:不再为盒子模型而烦恼
题外话: W3C奉行的标准,就是content-box,就是须要计算边框,填充还有内容的;可是就我个人而言, 比較喜欢的是传统IE6时候的怪异模式,不用考虑容器是否会被撑开(打乱布局); 盒子模型差异 ...
- Extjs TabPanel页签转换事件
listeners : { tabchange : function(tp, p) { var allmapDIV = document.getElementById("allmap&quo ...
- dp状态压缩
dp状态压缩 动态规划本来就很抽象,状态的设定和状态的转移都不好把握,而状态压缩的动态规划解决的就是那种状态很多,不容易用一般的方法表示的动态规划问题,这个就更加的难于把握了.难点在于以下几个方面:状 ...
- spring:为javabean的集合对象注入属性值
spring:为JavaBean的集合对象注入属性值 在 spring 中可以对List.Set.Map 等集合进行配置,不过根据集合类型的不同,需要使用不同的标签配置对应相应的集合. 1.创建 Ts ...
- Spark之Structured Streaming
目录 Part V. Streaming Stream Processing Fundamentals Structured Streaming Basics Event-Time and State ...
- XML案例(使用JAXP进行SAX解析)
1.Book.java package cn.itcast.sax; public class Book { private String name; private String author; p ...
- 一段时间加载的js函数
<html><head><meta charset="utf8"><script type="text/javascript&q ...
- Python 45 长度及颜色单位 、字体样式 、文本样式 、背景样式 、css基础选择器
一:长度及颜色单位 长度单位 px(像素) in(英寸) pt(点),一个标准的长度单位,1pt = 1/72in mm(毫米) cm ...
- Angular 显示英雄列表
在本页面,你将扩展<英雄指南>应用,让它显示一个英雄列表, 并允许用户选择一个英雄,查看该英雄的详细信息. 创建模拟(mock)英雄数据 你需要一些英雄数据以供显示. 最终,你会从远端的数 ...
- 数据通讯与网络 第五版第24章 传输层协议-UDP协议部分要点
24.1 介绍 本章节主要集中于传输层协议的解读,图24.1展示TCP.UDP.SCTP在TCP\IP协议栈的位置 24.1.1 服务(Service) 每个协议都提供不同的服务,所以应该合理正确的使 ...