遵循递归
我正在努力遵循这个“据称简单”的递归过程
void display(int x, int y) {
int[] a = {0,1,2,3};
if(x==y) {
System.out.print(a[x]+" ");
}
else {
int mid=(x+y)/2;
display( x, mid);
display( mid+1, y);
}
在第一个打印语句 x=0 和 y=0 和 mid=0 之后, - 我明白这一点。下一个调用似乎是第二个调用 display( mid+1, y);现在突然 y=1 - 这个变化发生在哪里 - 执行 print 语句,然后 y=3 的值。显然调试器不是遵循这个的最佳方法 - 我理解因子示例中发生了什么并且可以在笔和纸上跟随它 - 是否可以看到这个例子中发生了什么?任何帮助将不胜感激。
I'm struggling to follow this 'supposedly simple' recursive procedure
void display(int x, int y) {
int[] a = {0,1,2,3};
if(x==y) {
System.out.print(a[x]+" ");
}
else {
int mid=(x+y)/2;
display( x, mid);
display( mid+1, y);
}
After the first print statement x=0 and y=0 and mid=0 - this I understand. The next call appears to be the second call display( mid+1, y); now suddenly y=1 - where did this change happen - the print statement is executed and then the value of y=3.Obviously the debugger isn't the best way to follow this - I understand what is happening in the factoral examples and can follow it on pen and paper - is it possible to see whats going on in this example? Any help will be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该尝试在函数的开始处打印
x
、y
和mid
。您还可以复制粘贴,如 @Mehrdad 建议的那样,或绘制一棵树(一棵二叉树,因为每个调用都会导致 0 或 2 个以上的调用)。
剧透:
——
我最初写了一些类似的东西,然后认为你可能不需要它。现在我认为您是这样做的:
x
和y
是函数的参数。因此,每次调用都会有自己独立的x
和y
。在这种情况下,将有 7 个 x 和 7 个 y(检查树)。至于“跳跃”,事实证明我很难解释,抱歉,但它很简单:树的一个分支结束,另一个开始。 (因此,当
(1,1)
结束时,(0,[0],1)
也结束,并且(0,[1],3)
code> 进行第二次调用,(2,[2],3)
- 这是从 1 到 3 的“跳转”。)You should try printing
x
,y
andmid
at the beginning of your function.You could also copy-paste, as @Mehrdad suggested, or draw a tree (a binary tree since each call causes exactly 0 or 2 more calls).
Spoiler:
--
I originally wrote something along the lines of this, then thought you might not need it. Now I think you do:
x
andy
are parameters to your function. As such, each invocation will have its own, independentx
andy
. In this case there will be 7x
s and 7y
s (check with the tree).As for the "jump", it turned out to be too hard for me to explain, sorry, but it's, simply: one branch of the tree ending and another beginning. (So when
(1,1)
ends,(0,[0],1)
also ends and(0,[1],3)
makes its second call,(2,[2],3)
- this is the "jump" from 1 to 3.)它只是使用“二分搜索”算法按顺序遍历所有元素,然后打印出每个元素。我认为结果只是从索引
x
到y
的所有元素。请注意,
mid
这个词是一个提示:如果您取两个事物的平均值并得到中间值,那么您的原始变量可能是start
和end分别。因此,尝试重写它:
它应该看起来更明显一点:将数组分成两半,在每一半上调用自己,当一半的长度为 1 时,打印该位置的值。
It's just going through all the elements in order, using a "binary search" sort of algorithm, and printing out each element. I think the result is just all the elements from indices
x
toy
.Notice that the word
mid
is a hint: If you're taking the average of two things and getting the middle, then your original variables were probablystart
andend
, respectively. So try rewriting it:and it should look a little more obvious: You divide the array in half, call yourself on each half, and when the length of your half is 1, you print the value at that position.