递归写法:
function fblq(a){ if(a == 1 || a == 2){ return 1; }else{ return fblq(a -1) + fblq(a -2); } } alert(fblq(6));
非递归:
function fibonacci(n){ var a,b,res; a = b = 1; for(var i=3;i<=n;i++){ res = a + b; a = b; b = res; } return res; } alert(fibonacci(6));
时间: 2024-10-12 11:07:37