Java code is a simple recursive implementation

 1 public class Test {
 2     public static void main(String[] args) {
 3         System.out.println(getSum(5));
 4     }
 5     public static int getSum(int n) {
 6         if(n==1) {
 7             return 1;
 8         }else {
 9             return n*getSum(n-1);
10         }
11     }
12 }

 Fibonacci number, a way

Law 1, 1, 2, 3, 5, 8. The third number from the beginning of the first two numbers.

public class Test02 {
    public static void main(String[] args) {
        int[] arr=new int[20];
        arr[0]=1;
        arr[1]=1;
        for(int x=2;x<arr.length;x++) {
            arr[x]=arr[x-1]+arr[x-2];
        }
        System.out.println(arr[19]);
    }
}

Fibonacci number, two ways

public class Test03 {
    public static void main(String[] args) {
        System.out.println(get(20));
    }    
    private static int get(int n) {
        if(n==1||n==2) {
            return 1;
        }else {
            return get(n-1)+get(n-2);
        }
    }
}

 

Guess you like

Origin www.cnblogs.com/cincing/p/11620135.html