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

'''
Project Euler 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 + 5 + 4 + 4 = 19 letters used in total.
If all the numbers from 1 to 1000 (one thousand) inclusive
were written out in words, how many letters would be used?
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. Answer: 21124
''' n1 = ['', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
n2 = ['', 'eleven', 'twelve', 'thirteen','fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen']
n3 = ['', 'ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'] sum = '' #所有数字的英文表达累加 for i in range(1, 1001):
baiwei = i//100 #百位数
shiwei = (i - baiwei*100)//10 #十位数
gewei = i%10 #个位数
eng = '' #当前数字的英文写法 if i == 1000:
eng += 'onethousand'
elif i%100 == 0: #若为整百的
eng = n1[baiwei] + 'hundred'
elif i > 100:
eng = eng + n1[baiwei] + 'hundredand'
if shiwei == 1 and gewei != 0:
eng += n2[gewei]
else:
eng = eng + n3[shiwei] + n1[gewei]
elif i > 19:
eng = eng + n3[shiwei] + n1[gewei]
elif i > 10:
eng += n2[gewei]
elif i == 10:
eng += 'ten'
else:
eng += n1[gewei]
sum += eng #累加各个数字的英文表达 print(len(sum))

老实说,看到这题的时候,我心里其实是不愿意做的:对于我这样的算法渣,肯定得写出N个 if 和 elif。但没办法,只好硬着头皮写,错了好几回之后,总算是把所有情况都考虑到了。擦汗……

大致思路是:把数字挨个儿转换成英文表达方式,累加起来,然后用 len() 计算总字符的长度即可。

Python练习题 045:Project Euler 017:数字英文表达的字符数累加的更多相关文章

  1. Python练习题 005:三个数字由大到小排序输出

    [Python练习题 005]输入三个整数x,y,z,请把这三个数由小到大输出. ----------------------------------------------------------- ...

  2. Python练习题 001:4个数字求不重复的3位数

    听说做练习是掌握一门编程语言的最佳途径,那就争取先做满100道题吧. ----------------------------------------------------------------- ...

  3. Project Euler 98:Anagramic squares 重排平方数

    Anagramic squares By replacing each of the letters in the word CARE with 1, 2, 9, and 6 respectively ...

  4. Project Euler 92:Square digit chains 平方数字链

    题目 Square digit chains A number chain is created by continuously adding the square of the digits in ...

  5. Python: 去掉字符串中的非数字(或非字母)字符

    >>> crazystring = ‘dade142.;!0142f[.,]ad’ 只保留数字>>> filter(str.isdigit, crazystring ...

  6. jquery判断字符长度 数字英文算1字符 汉字算2字符

    <input type="text" maxlength="25" oninput="textlength(this)"> &l ...

  7. Python3练习题 001:4个数字求不重复的3位数

    #Python练习题 001:4个数字求不重复的3位数#方法一import itertoolsres = [][res.append(i[0]*100 + i[1]*10 + i[2]) for i ...

  8. Python练习题 039:Project Euler 011:网格中4个数字的最大乘积

    本题来自 Project Euler 第11题:https://projecteuler.net/problem=11 # Project Euler: Problem 10: Largest pro ...

  9. Python练习题 047:Project Euler 020:阶乘结果各数字之和

    本题来自 Project Euler 第20题:https://projecteuler.net/problem=20 ''' Project Euler: Problem 20: Factorial ...

随机推荐

  1. 软件人如何快速晋升CTO(一)

    1.场景描述 ​ 软件人如何快速晋升CTO? 实战操作,介绍下如何0成本拥有自己的软件公司,出任CTO/CEO. ​ 2020年 8月16日,软件老王拿到公司的营业执照和公章了,税务登记也一起办理好了 ...

  2. VUE响应式原理-如何追踪变化

    Vue 最独特的特性之一,是其非侵入性的响应式系统.数据模型仅仅是普通的 JavaScript 对象.而当你修改它们时,视图会进行更新.这使得状态管理非常简单直接 如何追踪变化 当你把一个普通的 Ja ...

  3. angular实现地区三级联动

    <!DOCTYPE html><html ng-app="myapp"> <head> <meta charset="UTF-8 ...

  4. 替换unimrcp的VAD模块

    摘要: unimrcp vad 模块voice activity dector一直认为比较粗暴,而且unimrcp的社区也很久没有更新了.使用原始unimrcp如果只是用来做Demo演示,通过手动调整 ...

  5. 关于js重名方法的先后调用问题

    当js中方法重名时,最后引入的js会覆盖前面的引入的js(就是说会调用最后引入的js中的方法)详情参照(main.js与white.js 的a())但是,当最后一个js中存在语法上的错误时(也可以是本 ...

  6. 蒲公英 &#183; JELLY技术周刊 Vol.21 -- 技术周刊 &#183; React Hooks vs Vue 3 + Composition API

    蒲公英 · JELLY技术周刊 Vol.21 选 React 还是 Vue,每个人心中都会有自己的答案,有很多理由去 pick 心水的框架,但是当我们扪心自问,我们真的可以公正的来评价这两者之间的差异 ...

  7. java 将map转为实体类

    使用反射将map转为对象,如果不使用反射的话需要一个get一个set写起来麻烦,并且不通用,所以写了一个通用的方法将map集合转为对象,直接看代码,注释也都挺清楚的 public static < ...

  8. IDAPython 安装和设置(windows+linux)

    安装步骤: 我采用的是IDA 6.8 windows安装: 机器上安装了Python,到Python的官网—http://www.python.org/getit/下载2.7的安装包.注意对应操作系统 ...

  9. Struts 2 漏洞专题 | S2-008

    漏洞简介 为了防止攻击者在参数内调用任意方法,默认情况下将标志xwork.MethodAccessor.denyMethodExecution设置为true,并将SecurityMemberAcces ...

  10. 2.AVFormatContext和AVInputFormat

    参考https://blog.csdn.net/leixiaohua1020/article/details/14214705 AVFormatContext: 用来存储视音频封装格式(flv,mp4 ...