英文:
Exercise java recursion
问题
我不理解这个练习如何返回数字的平方。特别是我不理解第六行,在该行中有一个返回语句,之后是"+2*x-1"。在那个调用中,程序的行为是什么?
英文:
I don't understand how this exercise return the number's square. In particular I don't understand the sixth line in which there is return statement and after this "+2*x-1". What is the program behavior in that call?
public class Es {
public static int RecCalc(int x) {
if (x==0) {
return 0;
}else {
return RecCalc(x - 1) + 2 * x - 1;
}
}
public static void main(String[] args) {
System.out.println(RecCalc(3));
}
}
答案1
得分: 4
我们可以通过一点代数知道这是如何运作的:
(x-1)² + 2x - 1
== x² - 2x + 1 + 2x - 1
== x²
如果您对(x + y)²
的公式不熟悉,那么您可以将(x-1)²
写成(x-1)*(x-1)
,然后使用FOIL方法或分配律。这留作读者的练习。
英文:
We can see how this works with a little algebra:
(x-1)² + 2x - 1
== x² - 2x + 1 + 2x - 1
== x²
If you are unfamiliar with the formula for (x + y)²
then you can do (x-1)²
by writing it as (x-1)*(x-1)
and using the FOIL method or the distributive property. This is left as an exercise for the reader.
答案2
得分: 1
如果你有4个物品,你可以做一个边长为2的正方形:
xx
xx
如果你想要一个边长为3的正方形,你需要9个物品:在每条边和底部各加2个物品,加上一个角落的物品:
xx.
xx.
..+
或者换个说法,每条边和底部各加3个物品,角落减少1个物品。
一般化来说,如果你有一个边长为(n-1)的正方形,想要制作一个边长为(n)的正方形,你需要添加2倍的(n-1)个物品,再加上一个;或者添加2倍的(n)个物品,然后减去一个。
因此:
边长为n的正方形中的物品数量
= 边长为(n-1)的正方形中的物品数量
+ 2 * (n-1) + 1
= 边长为(n-1)的正方形中的物品数量
+ 2 * n - 1
英文:
If you have 4 things, you can make a square with side 2:
xx
xx
If you want to make a square with side 3, you need 9 things: add 2 things on each of the side and bottom, plus 1 for the corner:
xx.
xx.
..+
Or, to put it another way, add 3 things on each of the side and bottom, take away 1 for the corner.
Generalizing, if you have a square of side length (n-1), to make a square of side length (n), you have to add on 2 lots of (n-1) things, plus one; or 2 lots of (n) things, take away one.
Hence:
number of things in a square of side length n
= (number of things in a square of side length (n-1))
+ 2 * (n-1) + 1
= (number of things in a square of side length (n-1))
+ 2 * n - 1
答案3
得分: 1
让我们逐步进行,一次调用一个。
第一个启动所有过程的调用是:
RecCalc(3);
在Java中,return语句将返回分号之前的所有内容。
因此,return 3 + 2
将返回5给调用者。
RecCalc(3)将导致调用:
RecCalc(2) + 2*3 -1;
RecCalc(2)将导致调用:
RecCalc(1) + 2*2 -1;
RecCalc(1)将导致调用:
RecCalc(0) + 2*1 - 1;
RecCalc(0)将返回0。
现在我们可以沿着调用堆栈返回。
RecCalc(0) == 0
RecCalc(1) == RecCalc(0) + 2*1 -1 == (0) + 2*1 -1 == 1
RecCalc(2) == RecCalc(1) + 2*2 -1 == (1) + 2*2 -1 == 4
RecCalc(3) == RecCalc(2) + 2*3 -1 == (4) + 2*3 -1 == 9
这并没有解释数学,但解释了递归。
让我们看一下数学部分。
正如@CodeApprentice解释的那样,x² = (x-1)² + 2x -1
整个递归方案的真正诀窍在于(x-1)²。
我们知道对于x = 4,我们可以使用(x-1)²再加上一些其他内容来获得答案。
但那只是3的平方再加上一些其他内容!
现在,为了得到3的平方,我们知道3² = (x-1)²再加上一些其他内容。
但那只是2的平方再加上一些其他内容!
因此,我们一直向下工作,直到得到一个简单的答案,其中我们返回0。(实际上,对于x=1,您也可以返回1)。
我希望这解释得清楚!
英文:
Let's step through, one call at a time.
The first call to kick it all off is:
RecCalc(3);
In Java, the return statement will take everything up to the semi-colon.
So, return 3 + 2
will return 5 to the caller.
RecCalc(3) will result in calling:
RecCalc(2) + 2*3 -1;
RecCalc(2) will result in calling:
RecCalc(1) + 2*2 -1;
RecCalc(1) will result in calling:
RecCalc(0) + 2*1 - 1;
RecCalc(0) will return 0.
Now we can work our way back up the call stack.
RecCalc(0) == 0
RecCalc(1) == RecCalc(0) + 2*1 -1 == (0) + 2*1 -1 == 1
RecCalc(2) == RecCalc(1) + 2*2 -1 == (1) + 2*2 -1 == 4
RecCalc(3) == RecCalc(2) + 2*3 -1 == (4) + 2*3 -1 == 9
This doesn't explain the math, but explains the recursion.
Let's look at the math.
As explained by @CodeApprentice, x² = (x-1)² + 2x -1
The real trick to this whole recursive scheme is the (x-1)².
We know that for x = 4, we can use (x-1)² plus some other junk to get the answer.
But that's just the square of 3 plus some other junk!
Now, to get the square of 3, we know that 3² = (x-1)² plus junk.
But that's just the square of 2 plus some other junk!
And so, we work our way down until we get to a trivial answer, where we return 0. (In fact, you could also return 1 for x=1).
I hope that explains it!
答案4
得分: 0
也许如果你加入一个打印语句会有帮助。
public static void main(String[] args) {
System.out.println(RecCalc(5));
}
public static int RecCalc(int x) {
if (x == 0) {
return 0;
} else {
int v = RecCalc(x - 1) + 2 * x - 1;
System.out.println((x-1) + " " + (2*x) + " " + (-1));
return v;
}
}
输出
0 2 -1
1 4 -1
2 6 -1
3 8 -1
4 10 -1
25
注意每行中最后两列的值之和都是奇数。而任意数量从1开始的连续奇数之和是一个完全平方数。因此实际上,这个方法就是对前 x
个奇数进行求和。
英文:
Perhaps if you put in a print statement it will help.
public static void main(String[] args) {
System.out.println(RecCalc(5));
}
public static int RecCalc(int x) {
if (x == 0) {
return 0;
} else {
int v = RecCalc(x - 1) + 2 * x - 1;
System.out.println((x-1) + " " + (2*x) + " " + (-1));
return v;
}
}
Prints
0 2 -1
1 4 -1
2 6 -1
3 8 -1
4 10 -1
25
Notice that the value of the sum of the last two columns in each line is an odd number. And the sum of any number of consecutive odd numbers starting with 1 is a perfect square. So essentially, this method just sums up the first x
odd numbers.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论