将python斐波那契代码转换为java?

将python斐波那契代码转换为java?,java,python,fibonacci,Java,Python,Fibonacci,下面是python代码: 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

下面是python代码:

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
代码的**部分(f2,f1=fibaux(n-1))在我的java代码中不正确。以下是java代码:

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};
 }
在python中,f2和f1是不同的值,但在我的java代码中,Q[]和R[]是相同的值,因此它不能计算正确的结果。我不知道如何使其工作?谢谢大家!

试试看:

public static int fib(int number){
  if (number == 0){
     return 0;
  }
  else{
     int fibauxArray[] = fibaux(number);
     return fibauxArray[0];
  }
}

public static int[] fibaux(int number){
  if (number == 1){
     return new int[] {1, 0};
  }
  else{
     int[] Q = fibaux(number-1);
     int f2 = Q[0];
     int f1 = Q[1];

     return new int[] {f2+f1, f2};
 }
}
尝试:


使用
类:

class Pair {
    private int first;
    private int second;
    // getters, setters, constructor
}
本部分:

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
致:


()

使用
类:

class Pair {
    private int first;
    private int second;
    // getters, setters, constructor
}
本部分:

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
致:


()

fibax返回索引0中的数组元素n和索引1中的数组元素n-1

不要执行fibaux两次。只使用第一次执行的结果

所以你应该这样做

else{
int[] Q = fibaux(number-1);
int f2 = Q[1] + Q[0];
int f1 = Q[0];
return new int[] {f2, f1};

fibaux返回索引0中的数组元素n和索引1中的数组元素n-1

不要执行fibaux两次。只使用第一次执行的结果

所以你应该这样做

else{
int[] Q = fibaux(number-1);
int f2 = Q[1] + Q[0];
int f1 = Q[0];
return new int[] {f2, f1};


为什么要在数组中返回单个值?另外,
R
从何而来?如果你只是想把Python翻译成Java,你应该保持相同的组织,比如公共静态int fib(int number)——为这两种情况返回一个数字,而不是数组,并且不需要Q和R,只需使用递归的一个结果,就像在Python中一样。我在处理Q[]类似于python代码中的f2和R[]类似于f1。是的,你是对的,fib()函数不需要返回数组。我不明白@cs,如果我去掉Q[]和R[],结果总是1?@Jessica看起来好像有人发布了一个完整的解决方案,但在看它之前,我会先考虑一下,因为很明显你不理解Python代码。让Fibax返回两个连续的Fibonacci值,然后对其进行操作以返回两个值。在Python中,“returnx,y”与Java“returnnewint[]{x,y}”基本相同。在Java代码中没有理由调用两次fibaux,因为唯一的区别是返回两个值的方式。为什么要在数组中返回一个值?另外,
R
从何而来?如果你只是想把Python翻译成Java,你应该保持相同的组织,比如公共静态int fib(int number)——为这两种情况返回一个数字,而不是数组,并且不需要Q和R,只需使用递归的一个结果,就像在Python中一样。我在处理Q[]类似于python代码中的f2和R[]类似于f1。是的,你是对的,fib()函数不需要返回数组。我不明白@cs,如果我去掉Q[]和R[],结果总是1?@Jessica看起来好像有人发布了一个完整的解决方案,但在看它之前,我会先考虑一下,因为很明显你不理解Python代码。让Fibax返回两个连续的Fibonacci值,然后对其进行操作以返回两个值。在Python中,“returnx,y”与Java“returnnewint[]{x,y}”基本相同。没有理由在Java代码中调用两次fibaux,因为唯一的区别是返回两个值的方式。谢谢!但是当我这样做时,它总是返回1,知道为什么吗?不管怎样,你必须使F2Q[1]和f1 Q[0]为我工作;对于Python代码的精确转换,+1应该更好。然而,在Java中使用递归斐波那契是一种低效、浪费内存的方法好吧,准确地说,它应该是
returnfibax(number)[0];-)谢谢但是当我这样做时,它总是返回1,知道为什么吗?不管怎样,你必须使F2Q[1]和f1 Q[0]为我工作;对于Python代码的精确转换,+1应该更好。然而,在Java中使用递归斐波那契是一种低效、浪费内存的方法好吧,准确地说,它应该是
returnfibax(number)[0];-)