简体   繁体   English

欧拉专案N#8 JAVA

[英]Project Euler N# 8 JAVA

I am trying to solve this problem and I think I am in the right track but for some reason the program does not run in a specific condition. 我正在尝试解决此问题 ,我认为我的方向正确,但是由于某种原因,程序无法在特定条件下运行。 This is the code: 这是代码:

public class Eight {
    public static void main(String[] args) {
        String set = "731671765313306249192251196744265747423"
                + "553491949349698352031277450632623957831801698"
                + "480186947885184385861560789112949495459501737958"
                + "331952853208805511125406987471585238630507156932909"
                + "632952274430435576689664895044524452316173185640309871"
                + "112172238311362229893423380308135336276614282806444486645"
                + "238749303589072962904915604407723907138105158593079608"
                + "66701724271218839987979087922749219016997208880937"
                + "7665727333001053367881220235421809751254540594752"
                + "243525849077116705560136048395864467063244157221"
                + "55397536978179778461740649551492908625693219784"
                + "686224828397224137565705605749026140797296865"
                + "241453510047482166370484403199890008895243450"
                + "6585412275886668811642717147992444292823086346567481391912316282458617866458"
                + "3591245665294765456828489128831426076900422421902267105562632111110937054421750694165"
                + "8960408071984038509624554443629812309878799272442849091888458015616609791913387549920052"
                + "4063689912560717606058861164671094050775410022569831552000559357297257163626956188267042"
                + "8252483600823257530420752963450";

        int initialIndex = 0;
        int lastIndex = 4;
        int finale = 0;

        for (;last <= set.length() - 1; initialIndex++, lastIndex++)
        {
            int num = Integer.parseInt(set.substring(initialIndex, lastIndex));
            int result = 1;

            while (num > 0)
            { 
                int digit = num % 10;
                result *= digit;
                num /= 10;
            }

            if (result > finale) 
                finale = result;
        } //end for 

        System.out.println(finale);
    }
}

When lastIndex equals 4, the result I get is 5832, which is the same result as Project Euler gives you as an example. 当lastIndex等于4时,我得到的结果是5832,这与Project Euler给出的结果相同。 But when Im trying to run this program with 13 numbers instead of 4, I get an exception and the program does not run. 但是,当我试图用13个数字而不是4个数字运行此程序时,出现异常,程序无法运行。

A 13 digit string will exceed the maximum allowable size of an int. 13位数字的字符串将超出int的最大允许大小。 Use Long.parseLong and change num from int to long . 使用Long.parseLong并将numint更改为long I did it, and got the following when I used 13 digits: 2091059712 我做到了,当我使用13位数字时得到以下信息:2091059712

Your issue begins here: 您的问题从这里开始:

            int num = Integer.parseInt(set.substring(initialIndex, lastIndex));

When you set lastIndex as 13, the number you are trying to take out of the string is 7,316,717,653,133. 当您将lastIndex设置为13时,您要从字符串中取出的数字是7,316,717,653,133。 In your code, you are trying to parse that String as an int , which has a maximum value of 2^31 2,147,483,647. 在您的代码中,您尝试将String解析为一个int ,最大值为2^31 2,147,483,647。

You can fix your problem by making any variable you expect to excede 2^31 a different integral data type, like a long . 您可以通过将期望超出2 ^ 31的任何变量设置为不同的整数数据类型(例如long

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM