家庭作业在Java中。找到五个连续数字最大的产品作业、数字、家庭、最大

2023-09-11 06:44:38 作者:自作多情、是一种贱毛病

我们已经开始在学校里学习Java,我们已经拿到了几个家庭作业要做。我已经成功地做​​到4 5,但最后一个是一个真正的痛苦。

We have started learning Java in school, and we have been given a few homeworks to do. I've managed to do 4 out of 5, but this last one is a real pain.

Basicly:编写一个程序,发现(在1000位长的数字)五个最大的产品 连续数字。

Basicly: Write a program that finds (in a 1000 places long number) the largest product of five consecutive digits.

下面是一些 http://pastebin.com/PFgL6jcM

你有任何想法如何解决这个问题?

Do you have any ideas how to solve this ?

如果这是不明确的,通知本人并会尝试向你解释了。

If this are unclear instruction, notify me and will try to explain to you again.

推荐答案

我会说一个优化的算法是这样的:

I'd say an optimized algorithm would look like this:

1)抢前五个数字

2)如果当前设置包含 0 ,在 0 抢了先五个数字。这样做,直到你到达一组不包含 0 。 (如果所有集合包含 0 - 不可能 - 符 0 )。

2) if current set contains a 0, grab the first five numbers after the 0. Do this until you reach a set that doesn't contain a 0. (if all sets contain a 0 - unlikely - return 0).

3)计算出5个号码( X1 X2 X3的产品 X4 X5 ),如下所示:

3) compute the product of the 5 numbers ( x1, x2, x3, x4, x5 ) like so:

p1 = x5 * x4
p2 = x3 * p1
p3 = x2 * p2
p  = x1 * p3

4)如果 P 大于previous P ,保存它。

4) if p is greater than the previous p, store it.

5)丢弃的第一个数字,并添加下一个( 5233 )。

5) discard the first number and add the next one (x6).

p = x6 * p3

6)如果新的 P 大于旧的,则转到步骤3)

6) if the new p is greater than the old one, go to step 3)

您正在减少乘法的数量由5倍,因为你不会让乘以5个号码,但2。

记住要丢弃包含 0 序列,并尝试优化沿着这些路线的算法。

Remember to discard sequences that contain a 0 and try to optimize the algorithm along these lines.