Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我能';使用Java开发EulerQ8项目时,我们没有得到正确的答案_Java - Fatal编程技术网

我能';使用Java开发EulerQ8项目时,我们没有得到正确的答案

我能';使用Java开发EulerQ8项目时,我们没有得到正确的答案,java,Java,有两个答案。第一个答案是4个连续的数字,答案是:5832。第二个答案是13个连续的数字,这是他们想要我输入的答案。答案是:23514624000 我对第一个问题的回答是:29760696,这是不可能的,而且还远远不够 public class test { public static void main(String[] args) { String big = "73167176531330624919225119674426574742355349194934" +

有两个答案。第一个答案是4个连续的数字,答案是:5832。第二个答案是13个连续的数字,这是他们想要我输入的答案。答案是:23514624000

我对第一个问题的回答是:29760696,这是不可能的,而且还远远不够

public class test {

public static void main(String[] args)
{



String big = "73167176531330624919225119674426574742355349194934"
        + "96983520312774506326239578318016984801869478851843"
        + "85861560789112949495459501737958331952853208805511"
        + "12540698747158523863050715693290963295227443043557"
        + "66896648950445244523161731856403098711121722383113"
        + "62229893423380308135336276614282806444486645238749"
        + "30358907296290491560440772390713810515859307960866"
        + "70172427121883998797908792274921901699720888093776"
        + "65727333001053367881220235421809751254540594752243"
        + "52584907711670556013604839586446706324415722155397"
        + "53697817977846174064955149290862569321978468622482"
        + "83972241375657056057490261407972968652414535100474"
        + "82166370484403199890008895243450658541227588666881"
        + "16427171479924442928230863465674813919123162824586"
        + "17866458359124566529476545682848912883142607690042"
        + "24219022671055626321111109370544217506941658960408"
        + "07198403850962455444362981230987879927244284909188"
        + "84580156166097919133875499200524063689912560717606"
        + "05886116467109405077541002256983155200055935729725"
        + "71636269561882670428252483600823257530420752963450"
        ;
//System.out.println(big.length());
int product=1;
int newProduct=0;
for(int i=0;i<big.length()-1-4;i++)
{
    product=1;
    for(int j=i+1;j<i+5;j++)
    {

        product=(big.charAt(i)-48)*(big.charAt(j)-48)*product;

    }

    if(product>newProduct)
    {
        newProduct=product;

    }
}


    System.out.println(newProduct);
}
公共类测试{
公共静态void main(字符串[]args)
{
String big=“73167176531330624919225119744265742355349194934”
+ "96983520312774506326239578318016984801869478851843"
+ "85861560789112949495459501737958331952853208805511"
+ "12540698747158523863050715693290963295227443043557"
+ "66896648950445244523161731856403098711121722383113"
+ "62229893423380308135336276614282806444486645238749"
+ "30358907296290491560440772390713810515859307960866"
+ "70172427121883998797908792274921901699720888093776"
+ "65727333001053367881220235421809751254540594752243"
+ "52584907711670556013604839586446706324415722155397"
+ "53697817977846174064955149290862569321978468622482"
+ "83972241375657056057490261407972968652414535100474"
+ "82166370484403199890008895243450658541227588666881"
+ "16427171479924442928230863465674813919123162824586"
+ "17866458359124566529476545682848912883142607690042"
+ "24219022671055626321111109370544217506941658960408"
+ "07198403850962455444362981230987879927244284909188"
+ "84580156166097919133875499200524063689912560717606"
+ "05886116467109405077541002256983155200055935729725"
+ "71636269561882670428252483600823257530420752963450"
;
//System.out.println(big.length());
int乘积=1;
int newProduct=0;

对于(int i=0;i您正在经历整数溢出(使用
long
)。
字符。数字(char,int)
(其中第二个参数是基数)可以获得
char
int
值。此外,您还可以使用
Math.max(long,long)
获得两个
long
的最大值.把这些放在一起,可能看起来像

long max = 0;
for (int i = 0; i < big.length() - 13; i++) {
    String str = big.substring(i, i + 13);
    long product = 1;
    for (char ch : str.toCharArray()) {
        product *= Character.digit(ch, 10);
    }
    max = Math.max(max, product);
}
System.out.println(max);

我的名字是“整数溢出”,我今天怎么能把你弄糊涂呢?嗨,整数溢出。如果你能告诉我你为什么把我弄糊涂那就太好了:我把你弄糊涂了,因为你可能认为我可以持有这个值
23514624000
,但我能持有的最大值是
2147483647
。大哥哥
long
的会议怎么样,它可以持有最大值你的答案已经提供了有效的代码,但是如果你想向OP解释他的算法哪里错了:
产品=(big.charAt(i)-48)*(big.charAt(j)-48)*产品;
这会增加
(big.charAt)(i)-48)
带有“其他数字”,这是错误的。它只能是
产品=(big.charAt(j)-48)*产品;
(或
产品*=(big.charAt(j)-48)
)。
long max = 0;
for (int i = 0; i < big.length() - 13; i++) {
    String str = big.substring(i, i + 13);
    long product = 1;
    for (char ch : str.toCharArray()) {
        product *= Character.digit(ch, 10);
    }
    max = Math.max(max, product);
}
System.out.println(max);
23514624000