项目欧拉任务#8在c++中,代码返回错误答案的微妙原因

Project Euler task #8 in c++, code returns wrong answer for subtle reason

本文关键字:答案 错误 返回 代码 任务 c++ 项目      更新时间:2023-10-16

我正在解决Project Euler问题,我已经提前到达了一个死胡同-我几乎可以肯定我的代码是正确的,但我的答案不被接受为正确的。

1000位号码中相邻的四位有最伟大的产品是9 × 9 × 8 × 9 = 5832。

7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450

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

我的方法很简单——这是我第一次在没有任何优化的情况下尝试这个问题,只是想看看我是否能解决它:

#include <string>
#include <iostream>
#include <stdlib.h> // atol ASCII to Integer function

int main(){
std::string digits =    "73167176531330624919225119674426574742355349194934";
            digits +=   "96983520312774506326239578318016984801869478851843";
            digits +=   "85861560789112949495459501737958331952853208805511";
            digits +=   "12540698747158523863050715693290963295227443043557";
            digits +=   "66896648950445244523161731856403098711121722383113";
            digits +=   "62229893423380308135336276614282806444486645238749";
            digits +=   "30358907296290491560440772390713810515859307960866";
            digits +=   "70172427121883998797908792274921901699720888093776";
            digits +=   "65727333001053367881220235421809751254540594752243";
            digits +=   "52584907711670556013604839586446706324415722155397";
            digits +=   "53697817977846174064955149290862569321978468622482";
            digits +=   "83972241375657056057490261407972968652414535100474";
            digits +=   "82166370484403199890008895243450658541227588666881";
            digits +=   "16427171479924442928230863465674813919123162824586";
            digits +=   "17866458359124566529476545682848912883142607690042";
            digits +=   "24219022671055626321111109370544217506941658960408";
            digits +=   "07198403850962455444362981230987879927244284909188";
            digits +=   "84580156166097919133875499200524063689912560717606";
            digits +=   "05886116467109405077541002256983155200055935729725";
            digits +=   "71636269561882670428252483600823257530420752963450";
    long product = 1;
    long temp = 1;
    for(int i = 12; i < digits.length(); i++){
        for(int j = 0; j<13; j++){
            temp = temp*atoi(digits.substr(i-j,1).c_str());
        }
        if(temp > product){
            product = temp;
            std::cout << "index: " << (i-12) << " Product: " << product << "n";
        }
        temp = 1;
    }
    return 0;
}

返回以下答案:

index: 0 Product: 5000940
index: 16 Product: 9797760
index: 19 Product: 13063680
index: 21 Product: 25401600
index: 22 Product: 50803200
index: 23 Product: 71124480
index: 24 Product: 284497920
index: 25 Product: 568995840
index: 40 Product: 1020366720
index: 41 Product: 1632586752
index: 43 Product: 2040733440
index: 503 Product: 2091059712

所以,当然,我把最后一个数字提交给体育网站,但被拒绝了。

我已经做了非常明显的完整性检查(索引503的13位数字实际上产生2091059712吗?),是的,它们确实产生了,所以我没有什么微妙的地方,它使索引出错,只乘以12个数字(或14),而不是正确的13。

所以…什么可能使我的代码返回错误的数字?我是不是不小心多乘了一个0 ?我已经看了一个小时了,现在是时候让其他人来看看我的代码了。

看来913不适合贵平台的long

您可以将long替换为long long int或其他更宽的整数。

实例给出如下输出

index: 0 Product: 5000940
index: 16 Product: 9797760
index: 19 Product: 13063680
index: 21 Product: 25401600
index: 22 Product: 50803200
index: 23 Product: 71124480
index: 24 Product: 284497920
index: 25 Product: 568995840
index: 40 Product: 1020366720
index: 41 Product: 1632586752
index: 43 Product: 2040733440
index: 88 Product: 2972712960
index: 195 Product: 6270566400
index: 196 Product: 14108774400
index: 197 Product: 23514624000

不犯这样的错误正是项目欧拉想教你的。