我正在尝试一个问题,我需要找出长整数中包含的2位数字的最大乘积:
Number = 73167176531330624919225119674426574742355349194934
lst = [int(i) for i in str(Number)]
print(lst)
new_lst = []
for i in lst:
new_lst.append(i*(i+1))
print(new_lst)
但是,我上面的尝试给出的结果为56,如何乘以7 * 3,然后乘以3 * 1,依此类推...
编辑H
Number = 7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450
lst = [int(i) for i in str(Number)]
print(lst)
new_lst = []
for i, value in enumerate(lst):
new_lst.append(lst[i-13]*(lst[i-12])*(lst[i-11])*(lst[i-10])*(lst[i-9])*(lst[i-8])*(lst[i-7])*(lst[i-6])*(lst[i-5])*(lst[i-4])*(lst[i-3])*(lst[i-2])*(lst[i-1]))
print(max(new_lst))
答案 0 :(得分:3)
您当前正在将数字乘以i +1。您可以使用range
来执行以下操作:
for i in range(len(lst)-1):
new_lst.append(lst[i]*(lst[i+1]))
print(new_lst)
答案 1 :(得分:2)
您可以遍历zip
列表及其一部分,其开头之间的差异为1:
max(a * b for a, b in zip(lst, lst[1:]))
这将返回:54
(由于数字9
和6
)
答案 2 :(得分:1)
itertools
有一个名为pairwise
的食谱。
>>> from itertools import tee
>>> def pairwise(iterable):
... "s -> (s0,s1), (s1,s2), (s2, s3), ..."
... a, b = tee(iterable)
... next(b, None)
... return zip(a, b)
...
>>> [a*b for a,b in pairwise(map(int, '1234'))]
[2, 6, 12]
答案 3 :(得分:1)
严格使用整数算术(而不是字符串)的版本:
Number = 73167176531330624919225119674426574742355349194934
max_prod, a, b = 0, None, None
Number, prev = divmod(Number, 10)
while Number:
Number, cur = divmod(Number, 10)
prod = cur * prev
if prod > max_prod:
max_prod, a, b = prod, cur, prev
# if prod == 81: # prod will never be bigger; can as well stop here.
# break
prev = cur
print(a, b, max_prod) # 9 6 54
更新以显示此方法如何解决当前的问题(您在其中查找13个相邻数字的最大乘积):
Number = int('731671765313306249192251196744265747423553491949349698352031277450632623957831801698480186947885184385861'
'560789112949495459501737958331952853208805511125406987471585238630507156932909632952274430435576689664895'
'044524452316173185640309871112172238311362229893423380308135336276614282806444486645238749303589072962904'
'915604407723907138105158593079608667017242712188399879790879227492190169972088809377665727333001053367881'
'220235421809751254540594752243525849077116705560136048395864467063244157221553975369781797784617406495514'
'929086256932197846862248283972241375657056057490261407972968652414535100474821663704844031998900088952434'
'506585412275886668811642717147992444292823086346567481391912316282458617866458359124566529476545682848912'
'883142607690042242190226710556263211111093705442175069416589604080719840385096245544436298123098787992724'
'428490918884580156166097919133875499200524063689912560717606058861164671094050775410022569831552000559357'
'2972571636269561882670428252483600823257530420752963450')
def digit_prod(i):
'''
return the product of all the decimal digits in i
'''
if i == 0:
return 0
prod = 1
while i:
i, r = divmod(i, 10)
if r == 0:
return 0
prod *= r
return prod
max_prod, res = 0, None
mod = 10**13
while Number:
i = Number % mod
prod = digit_prod(Number % mod)
if prod > max_prod:
max_prod, res = prod, i
# if prod == 9**13: # prod will never be bigger; can as well stop here.
# break
Number //= 10
print(res, max_prod) # 5576689664895 23514624000
答案 4 :(得分:1)
Number = 73167176531330624919225119674426574742355349194934
lst = [str(Number)中i的int(i)] 打印(第一)
new_lst = []
对于xrange(len(lst)-1)中的i: new_lst.append(lst [i] * lst [i + 1]) 打印new_lst
答案 5 :(得分:1)
您可能想要这样的东西:
Number = 73167176531330624919225119674426574742355349194934
print(max(int(x)*int(y) for x, y in zip(str(Number)[:-1], str(Number)[1:])))
答案将为54(9 * 6)
答案 6 :(得分:1)
您无需创建列表即可计算可迭代对象的最大值。这将是低效率的。这是使用max
,map
和mul
的功能解决方案:
from operator import mul
x = '73167176531330624919225119674426574742355349194934'
res = max(map(mul, map(int, x), map(int, x[1:]))) # 54