[Project Euler]Problem 8
时间:2011-05-21 来源:class
Problem 8:
Find the greatest product of five consecutive digits in the 1000-digit number.
问题:
找出1000个数字中连续5个数乘积的最大值
方法一:
由题意直接写代码:
from functools import reduce
from operator import mul
def f1(s):
l=list(map(int,''.join(s.split('\n'))))
return max(reduce(mul,l[i-4:i+1]) for i in range(4,len(l)))
print(f1(V))
优化:
发现0时可以跳过 ,第n个数比第n-5个数小时可以跳过。
def f2(s):
l=list(map(int,''.join(s.split('\n'))))
i=5
re=reduce(mul,l[0:5])
while i<len(l):
if l[i]>l[i-5]:
temp=reduce(mul,l[i-4:i+1])
if temp>re:
re=temp
if l[i]==0:
i+=4
i+=1
return re
相关阅读 更多 +