本题来自 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. luogu_P3373 solution

    luogu_P3373 solution Problme Description  Now, you have a known series, there are three operations: ...

  2. 力扣Leetcode 面试题56 - I. 数组中数字出现的次数

    面试题56 - I. 数组中数字出现的次数 一个整型数组 nums 里除两个数字之外,其他数字都出现了两次.请写程序找出这两个只出现一次的数字.要求时间复杂度是O(n),空间复杂度是O(1). 示例 ...

  3. 【Nginx】如何基于主从模式搭建Nginx+Keepalived双机热备环境?这是最全的一篇了!!

    写在前面 最近出版了<海量数据处理与大数据技术实战>,详情可以关注 冰河技术 微信公众号,查看<我的<海量数据处理与大数据技术实战>出版啦!>一文. 也有不少小伙伴 ...

  4. 10行Python代码自动清理电脑内重复文件,解放双手!

    大家好,又到了Python办公自动化系列. 今天分享一个系统层面的自动化案例: 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手.很多已经做 ...

  5. 虚拟机VmWare打开报错,错误提示:VMware Authorization Service is not running!

    作者:程序员小冰,CSDN博客:http://blog.csdn.net/qq_21376985 QQ986945193 微博:http://weibo.com/mcxiaobing 说明:打开我的虚 ...

  6. 深度学习论文翻译解析(十三):Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks

    论文标题:Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks 标题翻译:基于区域提议(Regi ...

  7. ASP.NET Core 进程内与进程外的性能对比

    ASP.NET Core 进程内与进程外的性能对比 本文内容是<深入去浅出ASP.NET Core>提供的扩展内容,毕竟在书里说进程内外的性能说明对比,对于初学者而言,稍微复杂了点. 我在 ...

  8. Activiti7 生成表结构

    首先创建一个Maven项目 整体的项目结构 activiti.cfg.xml配置文件 <?xml version="1.0" encoding="UTF-8&quo ...

  9. 20190928-01Redis五大数据类型之Hash和Zset 000 029

  10. 通过例子讲解Spring Batch入门,优秀的批处理框架

    1 前言 欢迎访问南瓜慢说 www.pkslow.com获取更多精彩文章! Spring相关文章:Springboot-Cloud相关 Spring Batch是一个轻量级的.完善的批处理框架,作为S ...