对于同一行中的循环不同的结束条件? [英] For loop different end conditions in the same line?

查看:125
本文介绍了对于同一行中的循环不同的结束条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设你想循环从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 完成递增。复杂性是孤立的,让循环终止在正确的时间,同时仍然允许 j 完成增量。



为了让循环终止,我们希望增加一个超过其最大值的较大的数字,以便达到终止标准。既然我们知道 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.

这篇关于对于同一行中的循环不同的结束条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆