<p>下面是python代码:</p>
<pre><code>def fib(n):
## Handle special case when n == 0
if n == 0:
return 0
## General case, return the first of the
## two values returned by fibaux
else:
return fibaux(n)[0]
## Auxiliary function
## Return the nth and (n-1)th Fibonacci numbers
## n must be an integer >= 1
def fibaux(n):
## Base case of for recursion
if n == 1:
return 1, 0
else:
## Recursive case
f2, f1 = fibaux(n - 1) ## **this is the part I cant figure out in java**
return f2 + f1, f2
</code></pre>
<p>代码的**部分(f2,f1=fibaux(n-1))在我的java中不正确代码。在这里是java代码:</p>
<pre><code>public static int[] fib(int number){
if (number == 0){
return new int[] {0};
}
else{
int fibauxArray[] = fibaux(number);
int f3 = fibauxArray[0];
return new int[] {f3};
}
}
public static int[] fibaux(int number){
if (number == 1){
return new int[] {1, 0};
}
else{
int[] Q = fibaux(number-1);
int[] R = fibaux(number-1);
int f2 = Q[0] + R[0];
int f1 = Q[0];
return new int[] {f2, f1};
}
</code></pre>
<p>在python中,f2和f1是不同的值,但在我的java代码中,Q[]和R[]是相同的值,因此它不能计算正确的结果。我不明白如何使这一点起作用?谢谢您!你知道吗</p>