系列中最大的产品-Project Euler#8

  • 本文关键字:-Project Euler#8 系列 c++
  • 更新时间 :
  • 英文 :


问题:

1000位数字中乘积最大的四个相邻数字是9×9×8×9=5832。

73167176531330624919225119674426574742355349194934969835203127745063262395783118069684801869478851843858615607891294949545950173795833195285320880551112540698774715852386305071569329096329522744304355766896648950445244521617318564030987111217223831136222989342380308135336276661428280644448666452387493035890729629049156044077239071381051585930796086670172427121883998799087922749219016997208880937766572733300105336788122023542180975125454059475224352584907711670551601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345658541227588666881164271714799244429282308634656748139191231628245861786645835912456652947654568284891288314260769004224219022671055626321111109370544217506941658960408071984038509962455444362981230987877992724428490918884580156166097919133875499200524063689912567176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450

找出1000位数字中乘积最大的13位相邻数字。这个产品的价值是多少?


(C++)

你好,

我写了一个代码,当我试图找到四个相邻数字的较大倍数时,它有效,但当我试图为13个相邻数字修改它时,它就停止了。我已经检查了很多次,不知道我做错了什么。它输出";2091059712";而不是";23514624000";谢谢你的帮助。

这是我编写的代码。

#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> v1 = {7,3,1,6,7,1,7,6,5,3,1,3,3,0,6,2,4,9,1,9,2,2,5,1,1,9,6,7,4,4,2,6,5,7,4,7,4,2,3,5,5,3,4,9,1,9,4,9,3,4,
9,6,9,8,3,5,2,0,3,1,2,7,7,4,5,0,6,3,2,6,2,3,9,5,7,8,3,1,8,0,1,6,9,8,4,8,0,1,8,6,9,4,7,8,8,5,1,8,4,3,
8,5,8,6,1,5,6,0,7,8,9,1,1,2,9,4,9,4,9,5,4,5,9,5,0,1,7,3,7,9,5,8,3,3,1,9,5,2,8,5,3,2,0,8,8,0,5,5,1,1,
1,2,5,4,0,6,9,8,7,4,7,1,5,8,5,2,3,8,6,3,0,5,0,7,1,5,6,9,3,2,9,0,9,6,3,2,9,5,2,2,7,4,4,3,0,4,3,5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6,4,5,8,3,5,9,1,2,4,5,6,6,5,2,9,4,7,6,5,4,5,6,8,2,8,4,8,9,1,2,8,8,3,1,4,2,6,0,7,6,9,0,0,4,2,
2,4,2,1,9,0,2,2,6,7,1,0,5,5,6,2,6,3,2,1,1,1,1,1,0,9,3,7,0,5,4,4,2,1,7,5,0,6,9,4,1,6,5,8,9,6,0,4,0,8,
0,7,1,9,8,4,0,3,8,5,0,9,6,2,4,5,5,4,4,4,3,6,2,9,8,1,2,3,0,9,8,7,8,7,9,9,2,7,2,4,4,2,8,4,9,0,9,1,8,8,
8,4,5,8,0,1,5,6,1,6,6,0,9,7,9,1,9,1,3,3,8,7,5,4,9,9,2,0,0,5,2,4,0,6,3,6,8,9,9,1,2,5,6,0,7,1,7,6,0,6,
0,5,8,8,6,1,1,6,4,6,7,1,0,9,4,0,5,0,7,7,5,4,1,0,0,2,2,5,6,9,8,3,1,5,5,2,0,0,0,5,5,9,3,5,7,2,9,7,2,5,
7,1,6,3,6,2,6,9,5,6,1,8,8,2,6,7,0,4,2,8,2,5,2,4,8,3,6,0,0,8,2,3,2,5,7,5,3,0,4,2,0,7,5,2,9,6,3,4,5,0,};

int max_product = 0, multiplication, i = 0;
while (i< size(v1) -13){
multiplication = v1[i] * v1[i+1] * v1[i+2] * v1[i+3]; //* v1[i+4] * v1[i+5] * v1[i+6] * v1[i+7] * v1[i+8] * v1[i+9] * v1[i+10] * v1[i+11] * v1[i+12];
i++;
if (multiplication>max_product){
max_product=multiplication;
}
}
// cout << "The greatest product is: " << max_product << endl;

return 0;
}

如果不应用注释中建议的优化(跳过序列中的零,并对13个整数窗口中最左边和最右边的数字进行除法/乘法运算),只需将所有整数类型的类型更改为uint64_t即可获得正确答案。

原因是有13个整数的序列,当相乘时,会溢出int的最大值。使用uint64_t,一种无符号的64位类型,不再有溢出的风险,因为9^13适合此整数类型。

以下是代码,其中有一些使用STL算法和数字函数的更改:

#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <functional>
int main() {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0, multiplication, i = 0;
while (i < size(v1) -13)
{
multiplication = std::accumulate(v1.begin() + i, 
v1.begin() + i + 13, 
uint64_t{1}, std::multiplies<uint64_t>());
max_product = std::max(multiplication, max_product);
i++;
}
std::cout << "The greatest product is: " << max_product << std::endl;
return 0;
}

输出:

The greatest product is: 23514624000

请注意std::accumulatestd::multiplies函数对象的用法,而不是您正在进行的长乘法序列。此外,std::max函数确定每次迭代期间的最大值。

最新更新