文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>[Project Euler]Problem 8

[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个数乘积的最大值

 方法一:
  由题意直接写代码:

View Code
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个数小时可以跳过。

View Code
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
相关阅读 更多 +
排行榜 更多 +
耶小兔子2

耶小兔子2

休闲益智 下载
nba2k20豪华版

nba2k20豪华版

体育竞技 下载
画线征服火柴人安卓版

画线征服火柴人安卓版

动作格斗 下载