本题来自 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. PyTorch入门-CIFAR10图像分类

    CIFAR10数据集下载 CIFAR10数据集包含10个类别,图像尺寸为 3×32×32 官方下载地址很慢,这里给一个百度云: https://pan.baidu.com/s/1oTvW8wNa-VO ...

  2. 2020.08.23 瞎扯周记之论短暂假期(QAQ)内要不要睡午觉

    蒟蒻的假期都是很短暂的嘛 作为一只合格的蒟蒻 假期自然是很短暂的QAQ 只有短短的26h93360s(手动微笑) 总体来讲 假期只有两件事要干: 1.满足人体自身需求 2.满足作业需求 2.5.摸鱼 ...

  3. mac android 真机调试

    1.已经安装好Androidstudio或者eclipse 2.下载配置好Android Sdk等 3.将android手机通过USB数据线连接Mac,打开终端输入system_profiler SP ...

  4. Mysql 部署

    设置 path 环境变量 C:\mysql\mysql-5.7.17-winx64\bin; 创建C:\mysql\mysql-5.7.17-winx64\my.ini 内容如下 [mysql] # ...

  5. Idiomatic Phrases Game(最短路+注意坑点)

    Tom is playing a game called Idiomatic Phrases Game. An idiom consists of several Chinese characters ...

  6. 15_Web框架-mini frame

    1.WSGI协议概述(Python Web Server Gateway Interface) 1.WSGI允许开发者将选择web框架和web服务器分开,可以混合匹配web服务器和web框架,选择一个 ...

  7. 读取文本文件中的中文打印到Eclipse控制台为何显示问号

    原因:未将文本文件存为utf-8编码格式而是ascii编码格式.

  8. SpringBoot搭建环境

    选择文件新建一个项目 选择:Spring Initializr,其他配置不变,点击下一步 这里一般写包名和项目名,这里我就默认,直接点击下一步 这里选择:Web  --> Spring Web ...

  9. cmd运行SpringBoot的jar中文乱码

    问题: 通过以下命令启动springBoot项目后,在项目中查看日志,发现中文乱码 java -jar projectName.jar 解决 启动时添加参数-Dfile.encoding=UTF-8即 ...

  10. Euclid's Game(POJ 2348)

    原题如下: Euclid's Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10832   Accepted: 4 ...