Project Euler Problem 8 (C++和Python代码实现和解析)

本文探讨了一个数学问题,即在一个1000位数中寻找具有最大乘积的13个连续数字。通过C++和Python代码实现,展示了如何计算这一序列的乘积,并验证了正确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

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

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

1. 第8个问题 : 一个序列里最大的乘积

在1000位的数里,4个相邻数字的最大乘积为9×9×8×9=5832。

在1000位的数里,找到13个相邻数字的最大乘积。乘积的值是多少?

2. 求解分析

这道题还是使用枚举,逐个计算n个相邻数字的乘积,然后保存最大的乘积。

如何保存或初始化1000位的数呢?两种方法,一种是数组(或vector),另外一种是字符串(string)。

如果是字符串的话,需要把数字字符转化为整数,然后才能计算乘积。

3. C++ 代码实现

在这里插入图片描述
类PE0008中包含一个成员变量vector m_digitsVector,一个方法 getGreatestProductOfAdjacentDigits(),还有构造函数PE0008()。

m_digitsVector用来保存1000位的数,每一位是m_digitsVector的一个元素。

构造函数PE0008()使用了STL里的函数transform(),把m_digitsVector里的数字字符(ASCII值)转化为数字整数。

函数getGreatestProductOfAdjacentDigits()用来在1000位数字中逐个计算n个相邻数字的乘积,然后找到最大的乘积。

C++ 代码

#include <iostream>
#include <vector>
#include <algorithm>   // transform()
#include <functional>  // bind2nd(), minus()
#include <cassert>     // assert() 

using namespace std;

const int max_digits = 1000;
    
class PE0008
{
private:
    vector<int> m_digitsVector;
    
public:
    PE0008(char digitsString[]);

    long long getGreatestProductOfAdjacentDigits(int numOfAdjacentDigits);
};

PE0008::PE0008(char digitsString[])
{
    // copy digits string to digits vector
    m_digitsVector.assign(digitsString, digitsString+max_digits);

    // covert each element in digits vector from char to integer
    transform(m_digitsVector.begin(), m_digitsVector.end(), 
              m_digitsVector.begin(), bind2nd(minus<int>(),'0'));
}

long long PE0008::getGreatestProductOfAdjacentDigits(int numOfAdjacentDigits)
{
    long long product;
    long long maxProduct = 1;

    for (unsigned int i=0; i+numOfAdjacentDigits<m_digitsVector.size(); i++)
    {
        product = 1;
        for (unsigned int k=i; k<i+numOfAdjacentDigits; k++)
        {
            product *= m_digitsVector[k];
        }
        if (product > maxProduct)
        {
            maxProduct = product;
        }
    }

    return maxProduct;
}

int main()
{
    static char digitsString[max_digits+1] = 
"73167176531330624919225119674426574742355349194934"
"96983520312774506326239578318016984801869478851843"
"85861560789112949495459501737958331952853208805511"
"12540698747158523863050715693290963295227443043557"
"66896648950445244523161731856403098711121722383113"
"62229893423380308135336276614282806444486645238749"
"30358907296290491560440772390713810515859307960866"
"70172427121883998797908792274921901699720888093776"
"65727333001053367881220235421809751254540594752243"
"52584907711670556013604839586446706324415722155397"
"53697817977846174064955149290862569321978468622482"
"83972241375657056057490261407972968652414535100474"
"82166370484403199890008895243450658541227588666881"
"16427171479924442928230863465674813919123162824586"
"17866458359124566529476545682848912883142607690042"
"24219022671055626321111109370544217506941658960408"
"07198403850962455444362981230987879927244284909188"
"84580156166097919133875499200524063689912560717606"
"05886116467109405077541002256983155200055935729725"
"71636269561882670428252483600823257530420752963450";

    PE0008 pe0008(digitsString);

    assert(5832 == pe0008.getGreatestProductOfAdjacentDigits(4));

    long long maxProduct = pe0008.getGreatestProductOfAdjacentDigits(13);
    
    cout << "The greatest product of the thirteen adjacent digits in the ";
    cout << "1000-digit number is " << maxProduct << "." << endl;

    return 0;    
}

4. Python 代码实现

Python在函数中使用文件PE0008.txt来保存1000位的数,把字符换转化为整数,最后把1000位数字保存在列表中。

函数getGreatestProductOfAdjacentDigits()用看来找到列表n位数字的最大乘积。

Python代码中有两行代码要仔细琢磨一下:

	return list(map(int, digitsString))

    maxProduct = max(maxProduct, reduce(mul, digits_list[i:i+n_digit]))

Python 代码

from operator import mul
from functools import reduce

def convertDigitsStringToIntList(filename):
    """  convert 1000 digits string to digit/integer list  """
    with open(filename) as file_object:
        lines = file_object.readlines()
    digitsString = ''
    for line in lines:
        digitsString += line.strip()
    return list(map(int, digitsString))

def getGreatestProductOfAdjacentDigits(digits_list, n_digit):
    """ n is number Of adjacent digits """
    maxProduct = 1
    for i in range (0, len(digits_list)-n_digit):
        maxProduct = max(maxProduct, reduce(mul, digits_list[i:i+n_digit]))
    return maxProduct

def main():
    digits_list = convertDigitsStringToIntList('PE0008.txt')
    assert 5832 == getGreatestProductOfAdjacentDigits(digits_list, 4)
    print("The greatest product of the thirteen adjacent digits in the ")
    print("1000-digit number is %d."\
          % getGreatestProductOfAdjacentDigits(digits_list, 13))

if  __name__ == '__main__':
    main()

PE0008.txt

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

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值