斐波那契数列具有一个通式:
当n>2时,f(n)=f(n-1)+f(n-2);当n=1或n=2时,f(1)=f(2)=1.
代码实现:
package com.lk.C;public class Test4 { public static int compute(int index){ if((index == 1)||(index == 2)){ return 1; }else{ return compute(index-1)+compute(index-2); } } public static void main(String[] args) { // TODO Auto-generated method stub System.out.println(compute(20)); }}
6765