gpt4 book ai didi

c# - 与嵌套的 for 循环作斗争

转载 作者:太空宇宙 更新时间:2023-11-03 19:49:42 25 4
gpt4 key购买 nike

给定一个主数和一个子集,想要找到子集数的所有可能组合,这些组合加起来等于主数。例如:

输入

主要:10
子集:2、3、5、7

输出

结果:3 + 7 = 10、2 + 3 + 5 = 10
** 注意:5 + 5 = 10 不是有效结果。 **

这是我目前所处的位置:

// Main traversal
for (int a = 0; a < nums.Length; a++)
{
sum = a;

for (int b = a + 1; b < nums.Length; b++)
{
// Thinking this should be the loop that determines
// how many numbers are added together.
// e.g. Sum of 2 numbers, sum of 3 numbers, etc
for (int c = a + 1; c < nums.Length; c++)
{
sum += nums[b + c];
}
if (sum == mainNumber)
result += "Match found!\n";
}
}

我主要是在努力理解“for 循环”。我试图找到一个“for 循环”可视化工具,但没有成功。或者,递归会使这个问题更容易解决吗?

最佳答案

将 for 循环想象成圆圈。嵌套循环就像圆圈中的圆圈。

Circles within circles

值得庆幸的是,您的情况远没有这个复杂。在这张图片中,周围的一个大圆圈是一个只为真一次的 for 循环。在该 for 循环内,其他循环多次为真。每个不同大小的圆圈都是一个不同的循环。

我已经用更具描述性的名称重写了您的代码,并且唯一的目的是解释循环 - 我取出了所有“有用”的东西,并放入了 console.writeline 语句。

 // I'm using 5 as an example check here.
for (int outerLoopCounter = 0; outerLoopCounter < 5; outerLoopCounter++)
{
Console.WriteLine("OUTER: {0}", outerLoopCounter);

for (int middleLoopCounter = 0; middleLoopCounter < 5; middleLoopCounter++)
{
Console.WriteLine("MIDDLE: {0}", middleLoopCounter);

for (int innerLoopCounter = 0; innerLoopCounter < 5; innerLoopCounter++)
{
Console.WriteLine("INNER: {0}", innerLoopCounter);
}
}
}

输出如下:

OUTER:             0
MIDDLE: 0
INNER: 0
INNER: 1
INNER: 2
INNER: 3
INNER: 4
MIDDLE: 1
INNER: 0
INNER: 1
INNER: 2
INNER: 3
INNER: 4
MIDDLE: 2
INNER: 0
INNER: 1
INNER: 2
INNER: 3
INNER: 4
MIDDLE: 3
INNER: 0
INNER: 1
INNER: 2
INNER: 3
INNER: 4
MIDDLE: 4
INNER: 0
INNER: 1
INNER: 2
INNER: 3
INNER: 4
OUTER: 1
MIDDLE: 0
INNER: 0
INNER: 1
INNER: 2
INNER: 3
INNER: 4
MIDDLE: 1
INNER: 0
INNER: 1
INNER: 2
INNER: 3
INNER: 4
MIDDLE: 2
INNER: 0
INNER: 1
INNER: 2
INNER: 3
INNER: 4
MIDDLE: 3
INNER: 0
INNER: 1
INNER: 2
INNER: 3
INNER: 4
MIDDLE: 4
INNER: 0
INNER: 1
INNER: 2
INNER: 3
INNER: 4
OUTER: 2
MIDDLE: 0
INNER: 0
INNER: 1
INNER: 2
INNER: 3
INNER: 4
MIDDLE: 1
INNER: 0
INNER: 1
INNER: 2
INNER: 3
INNER: 4
MIDDLE: 2
INNER: 0
INNER: 1
INNER: 2
INNER: 3
INNER: 4
MIDDLE: 3
INNER: 0
INNER: 1
INNER: 2
INNER: 3
INNER: 4
MIDDLE: 4
INNER: 0
INNER: 1
INNER: 2
INNER: 3
INNER: 4
OUTER: 3
MIDDLE: 0
INNER: 0
INNER: 1
INNER: 2
INNER: 3
INNER: 4
MIDDLE: 1
INNER: 0
INNER: 1
INNER: 2
INNER: 3
INNER: 4
MIDDLE: 2
INNER: 0
INNER: 1
INNER: 2
INNER: 3
INNER: 4
MIDDLE: 3
INNER: 0
INNER: 1
INNER: 2
INNER: 3
INNER: 4
MIDDLE: 4
INNER: 0
INNER: 1
INNER: 2
INNER: 3
INNER: 4
OUTER: 4
MIDDLE: 0
INNER: 0
INNER: 1
INNER: 2
INNER: 3
INNER: 4
MIDDLE: 1
INNER: 0
INNER: 1
INNER: 2
INNER: 3
INNER: 4
MIDDLE: 2
INNER: 0
INNER: 1
INNER: 2
INNER: 3
INNER: 4
MIDDLE: 3
INNER: 0
INNER: 1
INNER: 2
INNER: 3
INNER: 4
MIDDLE: 4
INNER: 0
INNER: 1
INNER: 2
INNER: 3
INNER: 4

如果你看一下缩进,你可以看到一个模式。最接近文本(左侧)的数字从外循环输出。中间的数字来自中间循环,最右边的数字来自最内层循环。

可以看到,外层循环在慢慢旋转,数到四(0, 1, 2, 3 , 4), 直到不再小于5,此时退出循环。中间的圆圈(循环)计数稍快。每当外循环递增 1 时,它就会变为 4(同样是 0-4)。内圈旋转得非常快 - 它数到四,每次 中间 圈递增时退出。

关于c# - 与嵌套的 for 循环作斗争,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41271009/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com