欧拉挑战赛8

时间:2015-01-19 00:10:18

标签: java

我一直在研究这个问题,我无法弄清楚为什么我的代码正在打印2091059712,这显然是不正确的。对于那些不知道的人,the challenge是要找到该长字符串中的13个相邻数字,它们乘以等于最高数字的数字。

package Project_Euler;

public class problem8 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //These 2 variables are chosen by the user. For the purpose of Euler Challenge, I have these values set as they are.
        int numToTest = 13;
        String num = "731671765313306249192251196744265747423553491949349698352031277450632623957831801698480186947885184385861560789112949" +
            "49545950173795833195285320880551112540698747158523863050715693290963295227443043557668966489504452445231617318564030987111" +
            "21722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121" +
            "88399879790879227492190169972088809377665727333001053367881220235421809751254540594752243525849077116705560136048395864467" +
            "06324415722155397536978179778461740649551492908625693219784686224828397224137565705605749026140797296865241453510047482166" +
            "37048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456" +
            "82848912883142607690042242190226710556263211111093705442175069416589604080719840385096245544436298123098787992724428490918" +
            "88458015616609791913387549920052406368991256071760605886116467109405077541002256983155200055935729725716362695618826704282" +
            "52483600823257530420752963450";

        //setting basic variables
        int test = numToTest - 1;
        int length = num.length();
        int[] array = new int[length];
        int result = 0;
        int[] testing = new int[numToTest];
        int product = 1;

        //Setting the array to have values equal to the corresponding values in the number above.
        for(int n = 0; n <= length - 1; n++){
            //The minus 48 is because the code adds 48 to the value for some reason
            array[n] = Integer.valueOf(num.charAt(n) - 48);
        }

        //Multiplies 13 digits together and tests which set comes out with the highest product
        for(int n = 0; n < length; n++){

            //assures that it only checks when there are enough digits to multiply together
            if(n + test < length){
                product = 1;

                //goes through the digits, multiplying them by the next one.
                for(int check = 0; check <= test; check++){
                        product *= array[n + check];
                        testing[check] = array[n + check];
                }
                //finds the greatest product
                if(product > result){
                    result = product;
                }
            }
        }
        //Prints the greatest product
        System.out.println(result);
    }

}

我已经检查过所涉及的数字是否正确,每个代码声明都是一样的。

2 个答案:

答案 0 :(得分:1)

int的结果和产品太大而不适合32位整数,选择使用longBigInteger(分别为64位和更多)。

你正在执行整数运算,你将13乘以;这样的产品会导致溢出到int,其中最大值约为20亿

答案 1 :(得分:0)

$( document ).ready(function() {
    //Your script
});