项目欧拉8

编程入门 行业动态 更新时间:2024-10-20 06:17:38
本文介绍了项目欧拉8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经阅读了关于项目Euler问题8的一些主题。但是我不知道为什么我的代码给了我错误的答案。

I've read some topics on the project Euler problem 8. However I don't know why my code is giving me the wrong answer.

package main; import java.util.ArrayList; public class Euler8 { public static void main(String[] args) { String bigNumber = "73167176531330624919225119674426574742355349194934" + "96983520312774506326239578318016984801869478851843" + "85861560789112949495459501737958331952853208805511" + "12540698747158523863050715693290963295227443043557" + "66896648950445244523161731856403098711121722383113" + "62229893423380308135336276614282806444486645238749" + "30358907296290491560440772390713810515859307960866" + "70172427121883998797908792274921901699720888093776" + "65727333001053367881220235421809751254540594752243" + "52584907711670556013604839586446706324415722155397" + "53697817977846174064955149290862569321978468622482" + "83972241375657056057490261407972968652414535100474" + "82166370484403199890008895243450658541227588666881" + "16427171479924442928230863465674813919123162824586" + "17866458359124566529476545682848912883142607690042" + "24219022671055626321111109370544217506941658960408" + "07198403850962455444362981230987879927244284909188" + "84580156166097919133875499200524063689912560717606" + "05886116467109405077541002256983155200055935729725" + "71636269561882670428252483600823257530420752963450" ; ArrayList<Integer> myArray = new ArrayList<Integer>(); int counter = 1; int current = 0; int product = 1; int maximumProduct = 0; for(int i = 0; i < bigNumber.length(); i++) { String b = "" + bigNumber.charAt(i); current = Integer.parseInt(b); myArray.add(current); if(counter % 5 == 0) { for(int x : myArray) { product = x * product; } if(product > maximumProduct) { maximumProduct = product; } myArray.clear(); product = 1; } counter++; } System.out.println(maximumProduct); } }

我得到答案31752和正确答案是因为这是一个家庭作业我无法复制问题的解决方案所以我想解释为什么我的代码不工作所以我可以解决它。

I'm getting the answer 31752 and the right one is 40824. Since this is for a homework I can't copy the solution to the problem so I would like any explanation on why my code is not working so I can fix it.

谢谢。

推荐答案

你没有考虑正确的元素乘。每个产品通过乘以5个连续数字获得,对于每个数字,下面的代码将这些数字保存在 myArray

You were not considering the right elements to multiply. Each product is obtained by multiplying 5 consecutive digits, for each digit the code below saves these digits in myArray

public static void main(String[] args) { String bigNumber = "73167176531330624919225119674426574742355349194934" + "96983520312774506326239578318016984801869478851843" + "85861560789112949495459501737958331952853208805511" + "12540698747158523863050715693290963295227443043557" + "66896648950445244523161731856403098711121722383113" + "62229893423380308135336276614282806444486645238749" + "30358907296290491560440772390713810515859307960866" + "70172427121883998797908792274921901699720888093776" + "65727333001053367881220235421809751254540594752243" + "52584907711670556013604839586446706324415722155397" + "53697817977846174064955149290862569321978468622482" + "83972241375657056057490261407972968652414535100474" + "82166370484403199890008895243450658541227588666881" + "16427171479924442928230863465674813919123162824586" + "17866458359124566529476545682848912883142607690042" + "24219022671055626321111109370544217506941658960408" + "07198403850962455444362981230987879927244284909188" + "84580156166097919133875499200524063689912560717606" + "05886116467109405077541002256983155200055935729725" + "71636269561882670428252483600823257530420752963450" ; ; ArrayList<Integer> myArray = new ArrayList<Integer>(); int counter = 1; int current = 0; int product = 1; int maximumProduct = 0; for(int i = 0; i < bigNumber.length(); i++) { String b = "" + bigNumber.charAt(i); current = Integer.parseInt(b); myArray.add(current); counter++; if(counter == 5) { for(int x : myArray) { product = x * product; } if(product > maximumProduct) { maximumProduct = product; } myArray.remove(0); product = 1; } counter=myArray.size(); } System.out.println(maximumProduct); } }

另请参阅 1000位数字中连续五位数的最新产品

更多推荐

项目欧拉8

本文发布于:2023-11-29 20:40:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647570.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:欧拉   项目

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!