【欧拉计划第 8 题】序列中最大的乘积 Largest product in a series

简介: 【欧拉计划第 8 题】序列中最大的乘积 Largest product in a series

Problem 8 Largest product in a series

The four adjacent digits in the 1000-digit number that have the greatest product are 9 ∗ 9 ∗ 8 ∗ 9 = 5832 9*9*8*9=58329989=5832.

Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?

问题 8 序列中最大的乘积

1000 位数字中乘积最大的相邻四个数字是 9 ∗ 9 ∗ 8 ∗ 9 = 5832 9*9*8*9=58329989=5832

找出 1000 位数字中乘积最大的 13 个相邻数字。这个数字的乘积是什么?

数字文件

73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450

思路分析

本题用 Python 效率会高一些,且思路更简单

我们将所有数字存放到本地的 txt 文件中,使用 Python 导入程序并存放在一个 List 中,此时,从 List 的第 0 位一直到最后,求出十三位数字的乘积并存方到另一个 List 中(结果集),在结果集中找出最大值并输出

下面附上两段代码供参考

代码实现

'''
Author: chenwr_727
Date: 2016-12-19 21:47:36
LastEditTime: 2022-04-15 17:29:02
'''
from functools import reduce
digit_str = '73167176531330624919225119674426574742355349194934' + \
    '96983520312774506326239578318016984801869478851843' + \
    '85861560789112949495459501737958331952853208805511' + \
    '12540698747158523863050715693290963295227443043557' + \
    '66896648950445244523161731856403098711121722383113' + \
    '62229893423380308135336276614282806444486645238749' + \
    '30358907296290491560440772390713810515859307960866' + \
    '70172427121883998797908792274921901699720888093776' + \
    '65727333001053367881220235421809751254540594752243' + \
    '52584907711670556013604839586446706324415722155397' + \
    '53697817977846174064955149290862569321978468622482' + \
    '83972241375657056057490261407972968652414535100474' + \
    '82166370484403199890008895243450658541227588666881' + \
    '16427171479924442928230863465674813919123162824586' + \
    '17866458359124566529476545682848912883142607690042' + \
    '24219022671055626321111109370544217506941658960408' + \
    '07198403850962455444362981230987879927244284909188' + \
    '84580156166097919133875499200524063689912560717606' + \
    '05886116467109405077541002256983155200055935729725' + \
    '71636269561882670428252483600823257530420752963450'
i = 0
n = 13
prod_12 = 0
prod_max = 0
while i + n < len(digit_str):
    if prod_12 == 0:
        if '0' not in digit_str[i: i+n]:
            prod_ = reduce(lambda x,y: int(x) * int(y), digit_str[i: i+n])
        else:
            i = digit_str.index('0', i) + 1
            continue
    else:
        if digit_str[i+n-1] == '0':
            prod_12 = 0
            i += n
            continue
        else:
            prod_ = prod_12 * int(digit_str[i+n-1])
    if prod_ > prod_max:
        prod_max = prod_
    prod_12 = prod_ / int(digit_str[i])
    i += 1
print(prod_max)
'''
Author: metaquant
Date: 2016-12-19 21:47:36
LastEditTime: 2022-04-15 17:29:02
'''
from functools import reduce
def main():
    with open('your file location','r') as f:
        data = ''
        for line in f.readlines():
            data = data + line.strip()
    res = []
    for i in range(988):
        sub = [int(x) for x in data[i:i+13]]
        prod = reduce(lambda x,y:x*y, sub)
        res.append(prod)
    return max(res)

答案:23514624000


相关文章
|
7月前
|
C++
【PTA】​L1-048 矩阵A乘以B​ (C++)
【PTA】​L1-048 矩阵A乘以B​ (C++)
78 0
【PTA】​L1-048 矩阵A乘以B​ (C++)
UVa787 - Maximum Sub-sequence Product(最大连续乘积子串)
UVa787 - Maximum Sub-sequence Product(最大连续乘积子串)
51 0
(二维vector)(绝对值求和等式的处理)B. Playing in a Casino
(二维vector)(绝对值求和等式的处理)B. Playing in a Casino
97 0
LeetCode 5340. 统计有序矩阵中的负数 Count Negative Numbers in a Sorted Matrix
LeetCode 5340. 统计有序矩阵中的负数 Count Negative Numbers in a Sorted Matrix
【欧拉计划第 11 题】 网格中的最大乘积 Largest product in a grid
【欧拉计划第 11 题】 网格中的最大乘积 Largest product in a grid
176 0
【欧拉计划第 11 题】 网格中的最大乘积 Largest product in a grid
【欧拉计划第 4 题】最大回文数乘积 Largest palindrome product
【欧拉计划第 4 题】最大回文数乘积 Largest palindrome product
121 0
【欧拉计划第 4 题】最大回文数乘积 Largest palindrome product
|
机器学习/深度学习
【欧拉计划第 6 题】和的平方与平方的和差值 Sum square difference
【欧拉计划第 6 题】和的平方与平方的和差值 Sum square difference
168 0
HDOJ(HDU) 2156 分数矩阵(嗯、求和)
HDOJ(HDU) 2156 分数矩阵(嗯、求和)
115 0

热门文章

最新文章