项目Euler#8,我不明白我在哪里错了 [英] Project Euler #8, I don't understand where I'm going wrong

查看:95
本文介绍了项目Euler#8,我不明白我在哪里错了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作项目欧拉问题第八位,其中香港专业教育学院已提供这个大的离谱号:

I'm working on project euler problem number eight, in which ive been supplied this ridiculously large number:

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

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

和我应该查找1000位数字十三个相邻数字具有最大的产品。例如第一个四个相邻数字的产品是7 * 3 * 1 * 6,
我的代码如下:

and am supposed to "Find the thirteen adjacent digits in the 1000-digit number that have the greatest product." EG the product of the first four adjacent digits is 7 * 3 * 1 * 6. My code is the following:

int main()
{
    string num = /* ridiculously large number omitted */;
    int greatestProduct = 0;
    int product;
    for (int i=0; i < num.length() -12; i++)
    {
        product = ((int) num[i] - 48);
        for (int j=i+1; j<i+13; j++)
        {
            product = product * ((int) num[j] - 48);
            if (greatestProduct <= product)
            {
                greatestProduct = product;
            }
        }
    }
    cout << greatestProduct << endl;
}



我不断收到2091059712作为项目欧拉告诉我的答案是错的,我怀疑它太大了。任何帮助将不胜感激。

I keep getting 2091059712 as the answer which project euler informs me is wrong and I suspect its too large anyway. Any help would be appreciated.

编辑:更改为unsigned long int它工作。感谢大家!

changed to unsigned long int and it worked. Thanks everyone!

推荐答案

事实上,您的解决方案太小而不是太大。答案是在注释中指出,有一个整数溢出,线索是事实,你的解决方案接近无符号整数的最大可能值:2147483647.你需要使用不同的类型来存储该产品。

In fact your solution is too small rather than too big. The answer is what was pointed out in the comments, that there is integer overflow, and the clue is in the fact that your solution is close to the largest possible value for an unsigned int: 2147483647. You need to use a different type to store the product.

请注意,下面的答案仍然是正确的,因为你的代码做错了,但它不是造成错误的值。尝试将您的(工作)代码 http://codereview.stackexchange.com ,如果你想让那里的人告诉你,你可以改进您的方法和您的编码风格。

Note that the answer below is still 'correct' in that your code does do this wrong, but it's not what is causing the wrong value. Try taking your (working) code to http://codereview.stackexchange.com if you would like the folks there to tell you what you could improve in your approach and your coding style.

您正在检查一个新的最伟大的产品内环而不是外环。这意味着你的最大值包括所有小于或等于13个数字的字符串,而不是只有13个字符。

You are checking for a new greatest product inside the inner loop instead of outside. This means that your maximum includes all strings of less or equal ton 13 digits, rather than only exactly 13.

这可能会有所不同,如果你找到一个字符串少于13个数字,有一个大的产品,但在任一端为0。你不应该将这个计算为最大,但是你的代码。 (我没有检查这是否确实发生了)。

This could make a difference if you are finding a string which has fewer than 13 digits which have a large product, but a 0 at either end. You shouldn't count this as the largest, but your code does. (I haven't checked if this does actually happen.)

for (int i=0; i < num.length() -12; i++)
{
    product = ((int) num[i] - 48);
    for (int j=i+1; j<i+13; j++)
    {
        product = product * ((int) num[j] - 48);
    }
    if (greatestProduct <= product)
    {
        greatestProduct = product;
    }
}

这篇关于项目Euler#8,我不明白我在哪里错了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆