2017-09-23 90 views
0

我想在不使用BigInteger类导入的情况下实现斐波那契序列,因此我重写了自己的添加方法,并花了两天时间,但我不知道为什么前6个数字的答案是正确的,其余答案与正确答案相反(例如,n = 7,我的答案是:31是正确的:13; n = 15,我的答案= 016,正确的一个= 610),当n变大时,答案就完全错误了(甚至不是正确答案的颠倒过程,这发生在n> = 25时)。 任何意见将不胜感激!使用递归实现斐波那契而不使用java.bignumber

以下是我的输出:

The 0th Fibonacci number is : 
    0 
    The 1th Fibonacci number is : 
    1 
    The 2th Fibonacci number is : 
    1 
    The 3th Fibonacci number is : 
    2 
    The 4th Fibonacci number is : 
    3 
    The 5th Fibonacci number is : 
    5 
    The 6th Fibonacci number is : 
    8 
    The 7th Fibonacci number is : 
    31 
    The 8th Fibonacci number is : 
    12 
    The 9th Fibonacci number is : 
    43 
    The 10th Fibonacci number is : 
    55 
    The 11th Fibonacci number is : 
    98 
    The 12th Fibonacci number is : 
    441 
    The 13th Fibonacci number is : 
    332 
    The 14th Fibonacci number is : 
    773 
    The 15th Fibonacci number is : 
    016 
    The 16th Fibonacci number is : 
    789 
    The 17th Fibonacci number is : 
    7951 
    The 18th Fibonacci number is : 
    4852 
    The 19th Fibonacci number is : 
    1814 
    The 20th Fibonacci number is : 
    5676 
    The 21th Fibonacci number is : 
    64901 
    The 22th Fibonacci number is : 
    11771 
    The 23th Fibonacci number is : 
    75682 
    The 24th Fibonacci number is : 
    86364 
    The 25th Fibonacci number is : 
    52047 
    The 26th Fibonacci number is : 
    393021 
    The 27th Fibonacci number is : 
    814491 
    The 28th Fibonacci number is : 
    118413 
    The 29th Fibonacci number is : 
    922905 
    The 30th Fibonacci number is : 
    040428 

而下面是我的代码:

package com.example.helloworld; 


import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.Collections; 

public class Fibonacci_Recursive{ 
    public static void main(String[] args) { 
     long start = System.nanoTime(); 
     long time = 0L; 
     for(int i = 0; time <= 60L; i++) 
     { 
      Fibonacci_Recursive fr = new Fibonacci_Recursive(i); 
      time = ((System.nanoTime() - start)/1000_000_000); 
     } 
    } 

    private Fibonacci_Recursive(int n){ 
     System.out.println("The " + n + "th Fibonacci number is :"); 
     if (n <= 1){ 
      System.out.println(n); 
     } 
     else { 
      int[] finalResult = getF(n); 
      String st = ""; 
      for (int i = 0; i < finalResult.length; i++){ 
       st = finalResult[i] + st; 
      } 
      System.out.println(st); 
     } 
    } 

    private int[] getF(int n){ 
     int[] head = new int[1]; 
     if (n <= 1) { 
      head[0] = n; 
      return head; 
     } 
     return add(getF(n - 1), getF(n - 2)); 
    } 

    private int[] add(int[] s1, int[] s2){ 
     int carrier = 0; 
     ArrayList<Integer> result = new ArrayList<>(); 
     int[] array1 = s1; 
     int[] array2 = s2; 
     array1 = reverseGeneralArray(array1); 
     array2 = reverseGeneralArray(array2); 
     int min = array2.length; 
     int min2 = array1.length; 

     if(min2 > min) { 
      for (int i = 0; i < min; i++) { 
       int x = array1[i] + array2[i]; 
       result.add((x + carrier) % 10); 
       carrier = x/10; 
      } 
      for (int j = 0; j <= min2 - min - 1; j++) { 
       int index = min; 
       result.add((array1[index] + carrier) % 10); 
       carrier = (array1[index] + carrier)/10; 
       index++; 
      } 
      if (carrier > 0) { 
       result.add(carrier); 
      } 
      Collections.reverse(result); 
      return convertIntegers(result); 
     } 
     else if(min2 < min) 
      { 
       for(int i = 0; i < min2; i ++){ 
        int x = array1[i] + array2[i]; 
        result.add((x + carrier) % 10); 
        carrier = x/10; 
       } 
       for(int j = 0; j <= min - min2 - 1; j++){ 
        int index = min2; 
        result.add((array2[index] + carrier) % 10); 
        carrier = (array2[index] + carrier)/10; 
        index++; 
       } 
       if (carrier > 0) { 
        result.add(carrier); 
       } 
       Collections.reverse(result); 
       return convertIntegers(result); 
      }else { 
       for (int i = 0; i < min; i++) { 
        int x = array1[i] + array2[i]; 
        result.add((x + carrier) % 10); 
        carrier = x/10; 
       } 
       if (carrier > 0) { 
        result.add(carrier); 
       } 
      Collections.reverse(result); 
      return convertIntegers(result); 
      } 

    } 

    private static int[] convertIntegers(ArrayList<Integer> integers) 
    { 
     int[] ret = new int[integers.size()]; 
     for (int i=0; i < integers.size(); i++) 
     { 
      ret[i] = integers.get(i); 
     } 
     return ret; 
    } 

    private int[] reverseGeneralArray(int[] x){ 
     int[] newX = new int[x.length]; 
     for(int i = 0; i < x.length; i++){ 
      newX[i] = x[x.length - i -1]; 
     } 
     return newX; 
    } 

} 
+1

什么BigNumber类? JDK中没有这样的类。你的意思是BigInteger?另外,为什么不使用它? – fge

+0

“BigInteger”首先使用一个“int”来处理数据。如果这个'int'溢出并且不能保存数据,则引入第二个'int'等等。非常直接,你不能以更好的方式实现这一点。所以你自己的想法最终可能会是一样的。 – Zabuza

+0

我很抱歉,我的意思是bigInteger .. – Richard

回答

0

你错过建设的结果,你从int[] finalResult拼接错误(反向)的方式String st

private Fibonacci_Recursive(int n) { 
    ... 
    for (int i = 0; i < finalResult.length; i++) { 
     //Replaced st = finalResult[i] + st by 
     st = st + finalResult[i]; 
    } 
    ... 
} 

额外:考虑,在环连接字符串时,因为串联拷贝整个字符串,使用StringBuilder

StringBuilder st = new StringBuilder(); 
for (int i = 0; i < finalResult.length; i++) { 
    st.append(finalResult[i]); 
} 

更新:从25开始,错误变得明显:承运人不正确时的总和两位数字等于10(74025而不是75025)。该缺陷是在add方法,其中,承运人应被计算为:

carrier = (x + carrier)/10; 

即:你必须要考虑到先前的载体。

+0

非常感谢你!!!!!!如果没有你的帮助,我永远也找不到.....我非常感谢你的额外建议,我将来会使用String builder。然而,我仍然无法找到n> = 25后得到错误答案的原因。你有任何线索吗? – Richard

+0

不客气。请检查更新后的答案,以便在n> = 25后查找错误原因。 –

+0

这就是要点!你怎么能这么快找到...你调试或使用其他东西? – Richard