Java 寻找连续数字的最大乘积(欧拉项目#8)

Java 寻找连续数字的最大乘积(欧拉项目#8),java,list,math,Java,List,Math,涉及在1000位数字中查找13个连续数字的最大乘积。我试图用下面的代码解决这个问题,但是我得到的结果太小,大约是10倍。我做错了什么 import java.util.Scanner; import java.util.*; public class eight { public static void main(String[] args) { String number="7316717653133062491922511967442657474235534919493496

涉及在1000位数字中查找13个连续数字的最大乘积。我试图用下面的代码解决这个问题,但是我得到的结果太小,大约是10倍。我做错了什么

import java.util.Scanner;
import java.util.*;
public class eight {
    public static void main(String[] args) {
    String number="7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450";
    String input = "782";
    int value = 782;
    List<Integer> x = new ArrayList<Integer>();
    for (char ch : number.toCharArray()){
        int element_add=Character.getNumericValue(ch);
       x.add(element_add);
    }
    int check_count=0;
    int multiply_value=0;
    int multiply_value_max=0;
    while(check_count<986){
    multiply_value=(x.get(check_count))*(x.get(check_count+1))*(x.get(check_count+2))*(x.get(check_count+3))*(x.get(check_count+4))*(x.get(check_count+5))*(x.get(check_count+6))*(x.get(check_count+7))*(x.get(check_count+8))*(x.get(check_count+9))*(x.get(check_count+10))*(x.get(check_count+11))*(x.get(check_count+12));
    if(multiply_value>multiply_value_max){
        multiply_value_max=multiply_value;
    }
    check_count++;
    }
    System.out.println(multiply_value_max);



    }

}
import java.util.Scanner;
导入java.util.*;
公共八班{
公共静态void main(字符串[]args){
串数="7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450";
字符串输入=“782”;
整数值=782;
列表x=新的ArrayList();
for(char ch:number.toCharArray()){
int element_add=Character.getNumericValue(ch);
x、 添加(元素添加);
}
整数检查计数=0;
int乘以_值=0;
int乘以_值_max=0;
while(检查\u count乘以\u值\u max){
乘_值_max=乘_值;
}
检查计数++;
}
System.out.println(乘以最大值);
}
}

您遇到的问题是,答案的上限可能大于int的最大值。因此,您需要使用类,或者至少是
long
,来存储结果。为了确保以足够的精度执行操作,您希望以相同的精度或将它们转换为相乘时的精度


通过使用for循环处理连续乘法,您还可以避免一些键入错误和样板错误。

任务/目标是什么?请在问题中包含问题陈述,并带有指向原始Project Euler问题的链接。问题是32位算术可能重复,而不是
getNumericValue
(对于字符
0
9
)应该可以很好地使用。保持产品运行可以节省更多的时间。在每个步骤中,除以
位[i-1]
,然后乘以
位[i+12]
。使用long仍然给出了错误的答案,您还有其他建议吗?@samrobbins我建议您调试代码。尝试使用一些较小的示例运行。当输入字符串为
“9999999999”时,您的代码是否输出2541865828329