我写了一个程序来检查我对纸上解决方案的想法是否正确(事实确实如此).
任务:在从10到200的所有数字相乘的后面有多少个零.
它是48,手动计算很简单.
我从来没有认真地写过python,这就是我得到的:
mul = 1
for i in range(10, 200 + 1):
mul *= i
string = str(mul)
string = string[::-1]
count = 0;
for c in str(string):
if c == '0':
count += 1
else:
break
print count
print mul
Run Code Online (Sandbox Code Playgroud)
我打赌有可能用像python这样的语言写出更优雅的东西.
ps:是的,这是一个家庭作业,但不是我的 - 我只是帮了一个人;-)
irr*_*ant 11
一个直接的实现,不涉及计算阶乘(因此它适用于大数,即2000000!)(已编辑):
fives = 0
twos = 0
for i in range(10, 201):
while i % 5 == 0:
fives = fives + 1
i /= 5
while i % 2 == 0:
twos = twos + 1
i /= 2
print(min(fives, twos))
Run Code Online (Sandbox Code Playgroud)
import math
answer = str(math.factorial(200) / math.factorial(9))
count = len(answer) - len(answer.rstrip('0'))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1056 次 |
| 最近记录: |