欧拉计划第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?

连续数字最大乘积

在下面这个1000位正整数中,连续4个数字的最大乘积是 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

找出这个1000位正整数中乘积最大的连续13个数字。它们的乘积是多少?

解题思路

没有想到什么比较好的办法只能枚举。

实现代码如下:

#include <bits/stdc++.h>
using namespace std;
string s = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450";
long long ans;
long long cal(int i) {
    long long res = 1;
    for (int j = 0; j < 13; j ++) res *= s[i+j] - '0';
    return res;
}
int main() {
    for (int i = 0; i+12 < 1000; i ++)
        ans = max(ans, cal(i));
    cout << ans << endl;
    return 0;
}

得到答案为 \(23514624000\)。

原文地址:https://www.cnblogs.com/quanjun/p/12322908.html

时间: 2024-08-06 13:20:57

欧拉计划第8题题解的相关文章

欧拉计划第3题题解

Largest prime factor The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143 ? 最大质因数 13195的所有质因数为5.7.13和29. 600851475143最大的质因数是多少? 解题思路 分解质因数的算法是 \(O( \sqrt{n} )\) 的算法. 求一个数 \(a\) 的质因数,可以从 \(2\) 开始到

欧拉计划第10题题解

Summation of primes The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two million. 素数的和 所有小于10的素数的和是2 + 3 + 5 + 7 = 17. 求所有小于两百万的素数的和. 解题思路 没有特别好的想法,下奶能想到的就是枚举算出200万以内的所有素数,然后求这些素数的和. 实现代码如下: #include <bits/st

欧拉计划第9题题解

Special Pythagorean triplet A Pythagorean triplet is a set of three natural numbers, \(a < b < c\) , for which, \(a^2 + b^2 = c^2\) For example, \(3^2 + 4^2 = 9 + 16 = 25 = 5^2\) . There exists exactly one Pythagorean triplet for which \(a + b + c =

欧拉计划第13题题解

Large sum Work out the first ten digits of the sum of the following one-hundred 50-digit numbers. 37107287533902102798797998220837590246510135740250 46376937677490009712648124896970078050417018260538 74324986199524741059474233309513058123726617309629

欧拉计划&#183;第十一题

题目11:在20×20的网格中同一直线上四个数的最大乘积是多少? 在以下这个20*20的网格中,四个处于同一对角线上的相邻数字用红色标了出来: 08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08 49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00 81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 6

欧拉计划&#183;第十题

题目10:计算两百万以下所有质数的和. 10以下的质数的和是2 + 3 + 5 + 7 = 17. 找出两百万以下所有质数的和. 源码 STDMETHODIMP COuLa::Test10(int number) { // TODO: 在此添加实现代码 __int64 sum = 2; for(int i = 2; 2*i-1 <= number; i++) { int c = 2*i-1; for( int j = 2;c>10? j<=10 : j<=c;j++) { if(1

欧拉计划&#183;第七题

题目7:找出第10001个质数. 前六个质数是2,3,5,7,11和13,其中第6个是13. 第10001个质数是多少? 源码 STDMETHODIMP COuLa::Test7(int number) { // TODO: 在此添加实现代码 int iNumberForCout = 1; int iNumberForOutput = 0; int iNumberForAdd = 1; while(iNumberForCout<number) { iNumberForAdd++; iNumber

欧拉计划&#183;第八题

题目8:找出这个1000位数字中连续13个数字乘积的最大值. 找出以下这个1000位的整数中连续13个数字的最大乘积. 73167176531330624919225119674426574742355349194934 96983520312774506326239578318016984801869478851843 85861560789112949495459501737958331952853208805511 1254069874715852386305071569329096329

欧拉计划&#183;第五题

题目5:找出最小的能被1-20中每个数整除的数. 2520是最小的能被1-10中每个数字整除的正整数. 最小的能被1-20中每个数整除的正整数是多少? 源码 STDMETHODIMP COuLa::Test5(int number) { // TODO: 在此添加实现代码 int iForNumber[MAX_PATH] = {0}; int iForFinalNumber[MAX_PATH] = {0}; for(int i = number; i>0; i--) { int c = i; i