递归函数打印了意外的大值。试图打印由指令要求的递归值。

huangapple go评论81阅读模式
英文:

Recursive Function Prints Large Unintended Values. Trying to Print Recursive Value Requested by Instructions.l

问题

下面是递归函数的调用:

public class formula {
    public static void main(String[] args) {
        int n = Integer.parseInt(args[0]);
        sierpinski(n);
    }
    
    public static void sierpinski(int n) {
        System.out.println(n);
        if (n == 0) {
            return;
        }
        else if (n > 0) {
            sierpinski((n-1));
        }
    }
}

当我将参数保持为 n-1 时,我理解它会正常倒计时,但当我将代码更改为:

sierpinski(3*(n-1));

我得到了下面提供的大数值。

C:\Users\joseph\Desktop\CS111 - 2020\cs111-a5>java formula.java 2
2
3
6
15
42
123
366
1095
3282
9843
29526
88575
265722
797163
2391486
7174455
21523362
64570083
193710246
581130735
1743392202
935209307
-1489339378

这是因为它每次倒计时都不断地乘以 3,直到增长到足够大的值以达到零以下并停止吗?如果是这样的话,我该如何输出使值匹配以下内容:

这些是指令:编写一个递归函数 sierpinski(),它接受一个参数 n,打印值 n,然后用值 n-1 调用自己三次。递归应在 n 变为 0 时停止。

这一步是为了帮助绘制完整的三角形,但我想在继续之前理解这个函数。谢谢您的帮助和解释。

sierpinski(0)

sierpinski(1)
1

sierpinski(2)
2
1
1
1

sierpinski(3)
3
2 1 1 1
2 1 1 1
2 1 1 1

sierpinski(4)
4
3 2 1 1 1 2 1 1 1 2 1 1 1
3 2 1 1 1 2 1 1 1 2 1 1 1
3 2 1 1 1 2 1 1 1 2 1 1 1

sierpinski(5)
5
4 3 2 1 1 1 2 1 1 1 2 1 1 1 3 2 1 1 1 2 1 1 1 2 1 1 1 3 2 1 1 1 2 1 1 1 2 1 1 1
4 3 2 1 1 1 2 1 1 1 2 1 1 1 3 2 1 1 1 2 1 1 1 2 1 1 1 3 2 1 1 1 2 1 1 1 2 1 1 1
4 3 2 1 1 1 2 1 1 1 2 1 1 1 3 2 1 1 1 2 1 1 1 2 1 1 1 3 2 1 1 1 2 1 1 1 2 1 1 1

希望这个翻译满足您的要求。如果您有任何其他需要,欢迎随时提问。

英文:

I am calling this recursive function below:

public class formula {
public static void main(String[] args) {
    int n = Integer.parseInt(args[0]);
    sierpinski(n);
}

public static void sierpinski(int n) {
    System.out.println(n);
    if (n == 0) {
        return;
    }
    else if (n > 0) {
        sierpinski((n-1));
    }
}

}

When I keep the argument as n-1 I understand that it would countdown normally but when I then change the code to

sierpinkski(3*(n-1));

I have the large values provided below.

C:\Users\joseph\Desktop\CS111 - 2020\cs111-a5>java formula.java 2
2
3
6
15
42
123
366
1095
3282
9843
29526
88575
265722
797163
2391486
7174455
21523362
64570083
193710246
581130735
1743392202
935209307
-1489339378

Is this because it is constantly multiplying by 3 each time it counts down and grows extremely large until there is a large enough value to get below zero and stop? If so how can I print it out so the values match this:

These are the instructions: Write a recursive function sierpinski() that takes one argument n, prints the value n, and then calls itself three times with the value n-1. The recursion should stop when n becomes 0.

This step is to help draw the full triangle but I want to understand the function before i continue. Thank you for your help and explanations.

sierpinksi(0)

sierpinksi(1)
1

sierpinski(2)
2
1
1
1

sierpinksi(3)
3
2 1 1 1
2 1 1 1
2 1 1 1

sierpinksi(4)
4
3 2 1 1 1 2 1 1 1 2 1 1 1
3 2 1 1 1 2 1 1 1 2 1 1 1
3 2 1 1 1 2 1 1 1 2 1 1 1

sierpinksi(5)
5
4 3 2 1 1 1 2 1 1 1 2 1 1 1 3 2 1 1 1 2 1 1 1 2 1 1 1 3 2 1 1 1 2 1 1 1 2 1 1 1
4 3 2 1 1 1 2 1 1 1 2 1 1 1 3 2 1 1 1 2 1 1 1 2 1 1 1 3 2 1 1 1 2 1 1 1 2 1 1 1
4 3 2 1 1 1 2 1 1 1 2 1 1 1 3 2 1 1 1 2 1 1 1 2 1 1 1 3 2 1 1 1 2 1 1 1 2 1 1 1

答案1

得分: 1

...然后用值n-1调用自身三次... 意味着你只需像下面这样连续三次复制对 sierpinski((n-1)) 的调用:

sierpinski((n-1));
sierpinski((n-1));
sierpinski((n-1));

或者你可以在循环中调用它们:

for (int i = 0; i < 3; i++) sierpinski(n-1);

然后你就会看到你想要看到的模式。

英文:

...and then calls itself three times with the value n-1... means you just need to duplicate calls to sierpinski((n-1)) one after the other 3 times as below:

sierpinski((n-1));
sierpinski((n-1));
sierpinski((n-1));

or you can call them in a loop

for ( int i = 0; i &lt; 3; i++ ) sierpinski(n-1);

then you will see the pattern you want to see

huangapple
  • 本文由 发表于 2020年10月28日 01:11:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/64559543.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定