gpt4 book ai didi

java - 我无法使用 Java 获得 Project Euler Q8 的正确答案

转载 作者:行者123 更新时间:2023-11-29 04:53:32 25 4
gpt4 key购买 nike

有 2 个答案。第一个答案是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);
}

我对第二个问题的回答是:2135048192 并且更接近。为什么会这样?我的代码如下。谢谢

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-13;i++)
{
product=1;
for(int j=i+1;j<i+14;j++)
{

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

}

if(product>newProduct)
{
newProduct=product;

}
}


System.out.println(newProduct);
}

最佳答案

您遇到整数溢出(使用 long)。 Character.digit(char,int)(其中第二个参数是基数)可以获得 charint 值。此外,您可以使用 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

关于java - 我无法使用 Java 获得 Project Euler Q8 的正确答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34572924/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com