所以我在看一个更有效的fibonacci计算器代码,但我不明白返回fibaux(n)[0]是什么意思。我正试着把它转换成java,我几乎拥有了它,但我不理解这部分。谢谢您!你知道吗
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)
return f2 + f1, f2
好的,谢谢大家!我现在明白了,但我想我在将其转换为java时没有走上正确的轨道,因为我没有得到正确的输出,这就是我写的:
public class Fibonacci {
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 f2 = Q[0]+Q[0];
int f1 = Q[0];
return new int[] {f2, f1};
}
}
有什么建议吗?再次感谢。你知道吗
fibaux
返回由两个数字组成的tuple(不可更改的列表):[0]
获取索引0处的项(第一个数字):我相信如果您要将其移植到Java,您要么希望使用
fibaux(n).get(0)
(iffibaux
返回List
)要么fibaux(n)[0]
(iffibaux
返回Array
)相关问题 更多 >
编程相关推荐