遵循递归

发布于 2024-10-31 10:27:30 字数 436 浏览 0 评论 0原文

我正在努力遵循这个“据称简单”的递归过程

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 技术交流群。

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

发布评论

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

评论(2

风蛊 2024-11-07 10:27:30

您应该尝试在函数的开始处打印 xymid

您还可以复制粘贴,如 @Mehrdad 建议的那样,或绘制一棵树(一棵二叉树,因为每个调用都会导致 0 或 2 个以上的调用)。

剧透:

             (0,[1],3)
            /         \
    (0,[0],1)         (2,[2],3)
   /         \       /         \
(0,0)       (1,1) (2,2)       (3,3)

——

我最初写了一些类似的东西,然后认为你可能不需要它。现在我认为您是这样做的:

xy 是函数的参数。因此,每次调用都会有自己独立的 xy。在这种情况下,将有 7 个 x 和 7 个 y(检查树)。

至于“跳跃”,事实证明我很难解释,抱歉,但它很简单:树的一个分支结束,另一个开始。 (因此,当 (1,1) 结束时,(0,[0],1) 也结束,并且 (0,[1],3) code> 进行第二次调用,(2,[2],3) - 这是从 1 到 3 的“跳转”。)

You should try printing x, y and mid 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:

             (0,[1],3)
            /         \
    (0,[0],1)         (2,[2],3)
   /         \       /         \
(0,0)       (1,1) (2,2)       (3,3)

--

I originally wrote something along the lines of this, then thought you might not need it. Now I think you do:

x and y are parameters to your function. As such, each invocation will have its own, independent x and y. In this case there will be 7 xs and 7 ys (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.)

森林散布 2024-11-07 10:27:30

它只是使用“二分搜索”算法按顺序遍历所有元素,然后打印出每个元素。我认为结果只是从索引 xy 的所有元素。

请注意,mid 这个词是一个提示:如果您取两个事物的平均值并得到中间值,那么您的原始变量可能是 startend分别。因此,尝试重写它:

void display(int start, int end)
{
    int[] a = { 0, 1, 2, 3 };
    if (start == end)
    {
        System.out.print(a[start] + " ");
    }
    else
    {
        int mid = (start + end) / 2;
        display(start, mid);
        display(mid + 1, 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 to y.

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 probably start and end, respectively. So try rewriting it:

void display(int start, int end)
{
    int[] a = { 0, 1, 2, 3 };
    if (start == end)
    {
        System.out.print(a[start] + " ");
    }
    else
    {
        int mid = (start + end) / 2;
        display(start, mid);
        display(mid + 1, end);
    }
}

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.

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