publicclassFoo{publicstaticvoidmain(String[]args){intresult1=0;intresult2=0;result1=diGui(30);result2=feiDiGui(30);System.out.println(递归结果:+result1);System.out.println(非递归结果:+result2);for(inti=1;i=30;i++){System.out.println(diGui(i)+----+feiDiGui(i));}}/***递归算法*/publicstaticintdiGui(inti){if(i=0){return0;}elseif(i=2){return1;}else{returndiGui(i-1)+diGui(i-2);}}/***非递归算法*/publicstaticintfeiDiGui(inti){if(i=0){return0;}elseif(i=2){return1;}else{int[]a=newint[i];//保存f(i)的值a[0]=1;a[1]=1;for(intj=3;j=i;j++){a[j-1]=a[j-2]+a[j-3];}returna[i-1];//返回数组中的最后一个数即f(i)}}}运行结果: