标签:
Fibnacci数列:如是:[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
f(0) = 1; f(1) = 1; f(n) = f(n-1)+f(n-2) n>1;
1 import java.util.Arrays; 2 3 public class Test { 4 5 public static void main(String[] args) { 6 Test test = new Test(); 7 int n = 15; 8 int[] fibnacciArray = new int[n]; 9 for (int i = 0; i < n; i++) { 10 fibnacciArray[i] = test.fibnacci(i);//将n处的值存入数组中 11 } 12 System.out.println(Arrays.toString(fibnacciArray)); 13 } 14 15 //递归求解n处的值 16 private int fibnacci(int n){ 17 if(n==0||n==1){ 18 return 1; 19 } 20 else if(n>1){ 21 return fibnacci(n-1)+fibnacci(n-2); 22 } 23 else{ 24 return 0; 25 } 26 } 27 }
输出:
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610]
标签:
原文地址:http://www.cnblogs.com/stivebone/p/5671684.html