将fibonacci存储在数组中并打印用户所需的值

时间:2016-02-10 15:55:59

标签: java fibonacci

我已经编写了一个用于打印斐波纳契系列的代码,我希望将其存储在动态数组中。 然后,当用户输入用于打印数组的特定元素的值时,它应显示存储在数组中的所需斐波那契数的值。

package fibonacci;

import java.util.Scanner;

public class Fibonacci {
    public static void main(String[] args){
        int size=new Scanner(System.in).nextInt();
        int fibonacci[]=new int[size];
        int i=3;

        System.out.println("The number");

        int number=new Scanner(System.in).nextInt();
        for(int i1=1;i1<=number;i1++){

            System.out.println(fibonacci(i1)+" ");

        }
        int j=new Scanner(System.in).nextInt();
        System.out.println(fibonacci[i]);

    }




        public static int fibonacci(int number){
                if(number==1||number==2){
                        return 1;

                    }
                    return fibonacci(number-1)+fibonacci(number-2);

            }
    }

2 个答案:

答案 0 :(得分:2)

只有大约94个斐波纳契值适合long,因此您可以在启动时创建这些值。同样使用循环来创建数字是O(n),但使用递归等于指数的答案。您可以快速获得需要比宇宙年龄更长的数字来计算。

答案 1 :(得分:1)

你可以利用数组并像这样使用它

fibonacci [i] = fibonacci [i-1] + fibonnaci [i-2]这将极大地改善代码的执行,因为递归函数可能具有高复杂性,您不会注意到小值的差异,但是像fibonacci这样的大价值[1000]你会注意到你可以让你的cpu和内存受到影响并让他们跪下的差异所以我会给你Fibonacci1这是你的版本和Fibonacci2更容易的版本:

<强> Fibonacci1

  import java.util.Scanner;

    public class Fibonacci1 {
        public static void main(String[] args) {
            System.out
                    .println("enter the upper bound of the serie(Size of the array of fibonacci serie):");
            Scanner scanner = new Scanner(System.in);
            int size = scanner.nextInt();
            int fibonacci[] = new int[size];

            for (int i = 0; i < size; i++) {
                fibonacci[i] = fibonacci(i+1);
            }

            System.out.println("enter a number between 1 and " + size);

            int j = scanner.nextInt();
            while (j < 0 || j > size) {
                if (j < 0) {
                    System.out.println("j<0");
                }
                if (j > size) {
                    System.out.println("j>" + size);
                }
                System.out.println("please enter a number between 1 and " + size);
            }

            System.out.println("Fibonacci[" + j + "]=" + fibonacci[j - 1]);
            scanner.close();
        }

        public static int fibonacci(int number) {
            if (number == 1 || number == 2) {
                return 1;
            }
            return fibonacci(number - 1) + fibonacci(number - 2);

        }
    }

<强> Fibonacci2:

package stackOverFlow;

import java.util.Scanner;

public class Fibonnaci {
    public static void main(String[] args) {
        System.out
                .println("enter the upper bound of the serie(Size of the array of fibonacci serie):");
        Scanner scanner = new Scanner(System.in);
        int size = scanner.nextInt();
        int fibonacci[] = new int[size];

        for (int i = 0; i < size; i++) {
            fibonacci[i] = fibonacci(i + 1, fibonacci);
        }

        System.out.println("enter a number between 1 and " + size);

        int j = scanner.nextInt();
        while (j < 0 || j > size) {
            if (j < 0) {
                System.out.println("j<0");
            }
            if (j > size) {
                System.out.println("j>" + size);
            }
            System.out.println("please enter a number between 1 and " + size);
        }
        System.out.println("Fibonacci[" + j + "]=" + fibonacci[j - 1]);
        scanner.close();
    }

    /*
     * changed the fibonacci function in order to use the already calculated
     * fibonacci elements
     * fibSerie[n]=fibonnaci[n-1]
     * fibSerie[n-1]=fibonnaci[n-2];
     * fibSerie[n-2]=fibonnaci[n-3];
     * because arrays start from 0 so fibSerie[1]=fibonnaci[0];
     */
    public static int fibonacci(int number, int[] fibonacci) {
        if (number == 1 || number == 2) {
            return 1;
        }
        return fibonacci[number - 2] + fibonacci[number - 3];

    }
}