将伪代码转换为尽可能相似的实现

发布于 2024-12-12 09:58:13 字数 1431 浏览 4 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

煮茶煮酒煮时光 2024-12-19 09:58:13

我想知道在用具体的编程语言实现算法时是否有更通用的方法/技巧来表示这一点

一般来说没有。没有办法将“负无穷大”表示为始终有效的整数。通常使用尽可能小的整数值(在 Java Integer.MIN_VALUE 中)。但你需要查看具体的算法来了解这个解决方案是否有效。

因此,没有通用/通用的解决方案。


在您的示例中,可以使用 Integer.MIN_VALUE 代替“负无穷大”。然而,
如果算法要求您使用Integer.MIN_VALUE作为实际值,那么您不能将其用作“无值”标记。 Integer.MIN_VALUE 不能作为“负无穷大”代理的另一种情况是当您使用它进行算术时。 (Integer.MIN_VALUE + x == Integer.MIN_VALUE 仅适用于 x == 0。)

I was wondering though if there is a more generic way/trick to represent this when implementing the algorithm in a concrete programming language

In general no. There is no way to represent "minus infinity" as an integer that will always work. The smallest possible integer value (in Java Integer.MIN_VALUE) is often used. But you need to look at the specific algorithm to understand if this solution works.

Hence there is no general / generic solution.


In your example Integer.MIN_VALUE can be used in place of "minus infinity". However,
if an algorithm requires you to use Integer.MIN_VALUE as an actual value, then you can't also use it as a "no value" marker. Another case where Integer.MIN_VALUE doesn't work as a proxy for "minus infinity" is when you use it to do arithmetic. (Integer.MIN_VALUE + x == Integer.MIN_VALUE only holds for x == 0.)

从来不烧饼 2024-12-19 09:58:13

您可以使用 Integer.MIN_VALUE这是最小的 32 位可表示值 (-2^31)。

或者,如果这实际上是您问题中的有效值,您可以扩展到 64 位并仅使用 Long.MIN_VALUE

最后,总是有一个 boolean firstTime = true; 选项,您可以在第一次时针对 if 条件|| 进行设置,并立即设置将其设置为false

You can use Integer.MIN_VALUE which is the smallest 32-bit representable value (-2^31).

Alternatively, if this could actually be a valid value in your problem, you could extend to 64 bits and just use Long.MIN_VALUE.

And finally, there's always the option of boolean firstTime = true; which you || against your if condition the first time around, and immediately set it to false.

不知所踪 2024-12-19 09:58:13
int current_sum = Integer.MIN_VALUE;

这给出了 current_sum int 可以具有的最小值,-(2^31)。

int current_sum = Integer.MIN_VALUE;

This gives current_sum the minimum value an int can have, -(2^31).

与之呼应 2024-12-19 09:58:13

通常,您用适合您的数据类型的最大值来表示无穷大。

例如,如果您有 +inifity,则可以使用 Integer.MAX_VALUE
与 -infinity 相同,您可以使用 Integer.MIN_VALUE

其他数据类型也是如此。

Normally you are representing infinity with the maximum that can fit in your datatype.

For example if you have +inifity, you can use Integer.MAX_VALUE.
Same with -infinity, you can use Integer.MIN_VALUE.

It's the same with other datatypes.

痞味浪人 2024-12-19 09:58:13

Integer.MIN_VALUEInteger.MAX_VALUE 不好的解决方案,因为在某些情况下它们可能会成为您域中的值。更仔细地使用Double.NEGATIVE_INFINITY

例如Double.NEGATIVE_INFINITY + 2 = Double.NEGATIVE_INFINITY

Integer.MIN_VALUE and Integer.MAX_VALUE bad solutions because in some cases they can become values in your domain. More carefully using Double.NEGATIVE_INFINITY.

For example Double.NEGATIVE_INFINITY + 2 = Double.NEGATIVE_INFINITY

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文