我可以理解多个递归的流程

发布于 2025-01-23 14:07:00 字数 278 浏览 0 评论 0原文

我不能指望此代码输出,我通常不了解递归,您能否编写此代码的流程或请说明,请提前。

static void m(int n) {
 if (n <= 0) {

 } else {
    m(n - 1);
    m(n - 2);
    System.out.println(n);
 }
}

    public static void main(String[] args) {
        m(5);
    }

}

I can't expect this code output, I generally don't understand recursion, can you write the flow of this code or explain please, thanks in advance.

static void m(int n) {
 if (n <= 0) {

 } else {
    m(n - 1);
    m(n - 2);
    System.out.println(n);
 }
}

    public static void main(String[] args) {
        m(5);
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

潜移默化 2025-01-30 14:07:00

代码始终是自上而下的(除非您有跳跃条件),

以便更好地理解这样的代码:

public static void main(String[] args) {
{
    m(5);
}

private static int callNumber = 0;

private static int GetCallNumber() {
    return ++callNumber;
}

private static void m(int n) {
    int callNumber = GetCallNumber();
    
    System.out.println(callNumber.ToString() + ": static void m(" + n.ToString() + ")");
    System.out.println(callNumber.ToString() + ": if (" + n.ToString() + " <= 0)");
    if (n <= 0) {
    } else {
        System.out.println(callNumber.ToString() + ": else");
        System.out.println(callNumber.ToString() + ": m(" + n.ToString() + " - 1)");
        m(n - 1);
        System.out.println(callNumber.ToString() + ": m(" + n.ToString() + " - 2)");
        m(n - 2);
        System.out.println(callNumber.ToString() + ": System.out.println(" + n.ToString() + ")");
        System.out.println(n);
    }

    System.out.println(callNumber.ToString() + ": //exit");
}
1: static void m(5)
1: if (5 <= 0)
1: else
1: m(5 - 1)
2: static void m(4)
2: if (4 <= 0)
2: else
2: m(4 - 1)
3: static void m(3)
3: if (3 <= 0)
3: else
3: m(3 - 1)
4: static void m(2)
4: if (2 <= 0)
4: else
4: m(2 - 1)
5: static void m(1)
5: if (1 <= 0)
5: else
5: m(1 - 1)
6: static void m(0)
6: if (0 <= 0)
6: //exit
5: m(1 - 2)
7: static void m(-1)
7: if (-1 <= 0)
7: //exit
5: System.out.println(1)
1
5: //exit
4: m(2 - 2)
8: static void m(0)
8: if (0 <= 0)
8: //exit
4: System.out.println(2)
2
4: //exit
3: m(3 - 2)
9: static void m(1)
9: if (1 <= 0)
9: else
9: m(1 - 1)
10: static void m(0)
10: if (0 <= 0)
10: //exit
9: m(1 - 2)
11: static void m(-1)
11: if (-1 <= 0)
11: //exit
9: System.out.println(1)
1
9: //exit
3: System.out.println(3)
3
3: //exit
2: m(4 - 2)
12: static void m(2)
12: if (2 <= 0)
12: else
12: m(2 - 1)
13: static void m(1)
13: if (1 <= 0)
13: else
13: m(1 - 1)
14: static void m(0)
14: if (0 <= 0)
14: //exit
13: m(1 - 2)
15: static void m(-1)
15: if (-1 <= 0)
15: //exit
13: System.out.println(1)
1
13: //exit
12: m(2 - 2)
16: static void m(0)
16: if (0 <= 0)
16: //exit
12: System.out.println(2)
2
12: //exit
2: System.out.println(4)
4
2: //exit
1: m(5 - 2)
17: static void m(3)
17: if (3 <= 0)
17: else
17: m(3 - 1)
18: static void m(2)
18: if (2 <= 0)
18: else
18: m(2 - 1)
19: static void m(1)
19: if (1 <= 0)
19: else
19: m(1 - 1)
20: static void m(0)
20: if (0 <= 0)
20: //exit
19: m(1 - 2)
21: static void m(-1)
21: if (-1 <= 0)
21: //exit
19: System.out.println(1)
1
19: //exit
18: m(2 - 2)
22: static void m(0)
22: if (0 <= 0)
22: //exit
18: System.out.println(2)
2
18: //exit
17: m(3 - 2)
23: static void m(1)
23: if (1 <= 0)
23: else
23: m(1 - 1)
24: static void m(0)
24: if (0 <= 0)
24: //exit
23: m(1 - 2)
25: static void m(-1)
25: if (-1 <= 0)
25: //exit
23: System.out.println(1)
1
23: //exit
17: System.out.println(3)
3
17: //exit
1: System.out.println(5)
5
1: //exit

Code is always processed top down (unless you have jump conditions)

For better understanding rewrite your code like this:

public static void main(String[] args) {
{
    m(5);
}

private static int callNumber = 0;

private static int GetCallNumber() {
    return ++callNumber;
}

private static void m(int n) {
    int callNumber = GetCallNumber();
    
    System.out.println(callNumber.ToString() + ": static void m(" + n.ToString() + ")");
    System.out.println(callNumber.ToString() + ": if (" + n.ToString() + " <= 0)");
    if (n <= 0) {
    } else {
        System.out.println(callNumber.ToString() + ": else");
        System.out.println(callNumber.ToString() + ": m(" + n.ToString() + " - 1)");
        m(n - 1);
        System.out.println(callNumber.ToString() + ": m(" + n.ToString() + " - 2)");
        m(n - 2);
        System.out.println(callNumber.ToString() + ": System.out.println(" + n.ToString() + ")");
        System.out.println(n);
    }

    System.out.println(callNumber.ToString() + ": //exit");
}
1: static void m(5)
1: if (5 <= 0)
1: else
1: m(5 - 1)
2: static void m(4)
2: if (4 <= 0)
2: else
2: m(4 - 1)
3: static void m(3)
3: if (3 <= 0)
3: else
3: m(3 - 1)
4: static void m(2)
4: if (2 <= 0)
4: else
4: m(2 - 1)
5: static void m(1)
5: if (1 <= 0)
5: else
5: m(1 - 1)
6: static void m(0)
6: if (0 <= 0)
6: //exit
5: m(1 - 2)
7: static void m(-1)
7: if (-1 <= 0)
7: //exit
5: System.out.println(1)
1
5: //exit
4: m(2 - 2)
8: static void m(0)
8: if (0 <= 0)
8: //exit
4: System.out.println(2)
2
4: //exit
3: m(3 - 2)
9: static void m(1)
9: if (1 <= 0)
9: else
9: m(1 - 1)
10: static void m(0)
10: if (0 <= 0)
10: //exit
9: m(1 - 2)
11: static void m(-1)
11: if (-1 <= 0)
11: //exit
9: System.out.println(1)
1
9: //exit
3: System.out.println(3)
3
3: //exit
2: m(4 - 2)
12: static void m(2)
12: if (2 <= 0)
12: else
12: m(2 - 1)
13: static void m(1)
13: if (1 <= 0)
13: else
13: m(1 - 1)
14: static void m(0)
14: if (0 <= 0)
14: //exit
13: m(1 - 2)
15: static void m(-1)
15: if (-1 <= 0)
15: //exit
13: System.out.println(1)
1
13: //exit
12: m(2 - 2)
16: static void m(0)
16: if (0 <= 0)
16: //exit
12: System.out.println(2)
2
12: //exit
2: System.out.println(4)
4
2: //exit
1: m(5 - 2)
17: static void m(3)
17: if (3 <= 0)
17: else
17: m(3 - 1)
18: static void m(2)
18: if (2 <= 0)
18: else
18: m(2 - 1)
19: static void m(1)
19: if (1 <= 0)
19: else
19: m(1 - 1)
20: static void m(0)
20: if (0 <= 0)
20: //exit
19: m(1 - 2)
21: static void m(-1)
21: if (-1 <= 0)
21: //exit
19: System.out.println(1)
1
19: //exit
18: m(2 - 2)
22: static void m(0)
22: if (0 <= 0)
22: //exit
18: System.out.println(2)
2
18: //exit
17: m(3 - 2)
23: static void m(1)
23: if (1 <= 0)
23: else
23: m(1 - 1)
24: static void m(0)
24: if (0 <= 0)
24: //exit
23: m(1 - 2)
25: static void m(-1)
25: if (-1 <= 0)
25: //exit
23: System.out.println(1)
1
23: //exit
17: System.out.println(3)
3
17: //exit
1: System.out.println(5)
5
1: //exit
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文