对于同一行中的循环不同的结束条件?

编程入门 行业动态 更新时间:2024-10-08 14:36:40
本文介绍了对于同一行中的循环不同的结束条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 假设你想循环从i = 0到i = n和j = 0到j = m ,并且 m!= n 。是否有可能缩短以下两个循环为一个?

for(int i = 0; i <= n; i ++) {} for(int j = 0; j <= m; j ++){}

实质上,我希望循环说增加i和j一,如果i = n,则停止递增i,如果m> n则继续递增j,或者如果 n> m ,则停止递增j

可能看起来微不足道,但我很好奇。

解决方案

天真地,我们想要做一些事情: (int i = 0,j = 0; i <= n || j <= m; i =数学公式.min(n,i + 1),j = Math.min(m,j + 1))

...但这不会终止,因为 i 的最大值是 n ,最大值对于 j 是 m ,其中之一将始终为真。

如果你愿意让( n<我们可以这样写:) i 在 n + 1 >

for(int i = 0,j = 0; i <= n || j <= m; i = Math.min (n + 1,i + 1),j = Math.min(m + 1,j + 1))

只有当你想保持( n

为了让循环终止,我们希望增加一个超过其最大值的较大的数字,以便达到终止标准。既然我们知道 i <= n 和 j <= m 中的至少一个将始终为真,那么让我们专注于使 始终为真,并将终止条件改为

i <= n& amp ;&安培; j <= m

在 n < m , i 会在 j 之前完成递增,所以我们需要让 j 为了违反 i< = n&& j <= m 。类似的情况适用于 n> m ,但相反,我们需要增加 i >过去 n 。

注意,如果 n == m ,我们可以安全地递增他们各自的限制,并终止标准将同时打。下面的循环处理任何正面输入 n 或 m ,并根据您的条件正确终止,同时允许 n 或 m 中的较小者成为相应迭代器的最大值。 (int i = 0,j = 0,nadj = n +(n> =m≥1:0),madj = m +( (nadj,i + 1),j = Math.min(n = 1,2,...,n) madj,j + 1))

值得注意的是,我们计算 nadj 和 madj ,以避免在每次迭代过程中重新计算它们。

Suppose you want to loop from i=0 to i=n and j=0 to j=m and that m!=n. Is it possible to shorten the following two loops into one?

for(int i=0; i<=n; i++){} for(int j=0; j<=m; j++){}

To something along

for(int i=0,j=0; i<=n, j<=m; i++, j++){}

In essence I want the loop to say "increment both i and j by one, stop incrementing i if i=n but keep incrementing j if m>n" or the other way around if n>m.

May seem trivial or stupid but I am curious.

解决方案

Naively, we want to do something like:

for(int i = 0, j = 0; i <= n || j <= m; i = Math.min(n, i+1), j = Math.min(m, j+1))

...but this won't terminate, because the maximum value for i is n and the maximum value for j is m, and one of those will always be true.

The problem is much simpler if you're willing to let (for n < m) i finish at n+1, as we could write:

for(int i = 0, j = 0; i <= n || j <= m; i = Math.min(n+1, i+1), j = Math.min(m+1, j+1))

This is complicated only if you want to keep (for n < m) i = n while j finishes incrementing. The complexity is isolated to getting the loop to terminate at the right time, while still allowing j to finish incrementing.

To get the loop to terminate, we want to increment the larger number one step past its maximum, so that we hit the termination criteria. Since we know at least one of i <= n and j <= m will always be true, let's focus on making both always true, and change our termination criteria to

i <= n && j <= m

In the case where n < m, i will finish incrementing before j, and so we need to let j increment one past its effective maximum in order to violate i <= n && j <= m. A similar condition holds for n > m, but instead, we need to increment i one past n.

Notice, though, that if n == m, we can safely increment both one past their respective limits, and the termination criteria will hit at the same time. The loop below handles any positive input n or m and terminates correctly given your conditons, while allowing the lesser of n or m to become the maximum value for the respective iterator.

for(int i = 0, j = 0, nadj = n + (n >= m ? 1 : 0), madj = m + (m >= n ? 1 : 0) i <= n && j <= m; i = Math.min(nadj, i+1), j = Math.min(madj, j+1))

Worth noting, we compute nadj and madj in the first section to avoid recomputing them during every iteration.

更多推荐

对于同一行中的循环不同的结束条件?

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

发布评论

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

>www.elefans.com

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