斐波那契数列(Fibonacci sequence),又称黄金分割数列、因数学家莱昂纳多·斐波那契(Leonardoda Fibonacci)以兔子繁殖为例子而引入,故又称为“兔子数列”,指的是这样一个数列:0、1、1、2、3、5、8、13、21、34、……在数学上,斐波那契数列以如下被以递推的方法定义:F(1)=1,F(2)=1, F(n)=F(n - 1)+F(n - 2)(n ≥ 3,n ∈ N*)在现代物理、准晶体结构、化学等领域,斐波纳契数列都有直接的应用
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>递归-排序</title> </head> <body> <script> function fn(n){ if(n<=1){ return 1; } return fn(n-1)+fn(n-2) } console.log(fn(0)) //1 console.log(fn(1)) //1 console.log(fn(2)) //2 console.log(fn(3)) //3 console.log(fn(4)) //5 console.log(fn(5)) //8 //console.log(fn(6)) //13 //console.log(fn(46)) //栈溢出,无法得到结果复制代码 </script> <script> function myFn(n){ if(typeof n !== "number" || n<1){ return n; }else if(n<=2){ return 1; } return myFn(n-1)+myFn(n-2) } //使用循环迭代方式实现 function fb2(n){ var res1 = 1; var res2 = 1; var sum = res2; for(var i = 2;i < n;i ++){ sum = res1 + res2; res1 = res2; res2 = sum; } return sum; } //闭包实现方式 const fb3 = function(){ var mem = [0,1]; var f = function(n){ var res = mem[n]; if(typeof res !== 'number'){ mem[n] = f(n-1) + f(n-2); res = mem[n]; } return res; } return f; }(); </script> <!-- 数组 --> <script> var arr = [1,2,3,"a","h",3,22,55,8]; console.log(arr.length); // console.log(arr.length =3); console.log(arr); console.log(arr.indexOf(1)); //返回索引 console.log(arr.indexOf(10)); // 找不到返回-1 // console.log(arr.sort()); console.log(arr.slice(4,6)); // console.log(arr.splice(2,1)) console.log(arr.splice(1)); </script> <!-- 排序 --> <script> // var arr = [12,34,3,24,78,90,16,45], temp = 0; // for(var i=0; i<arr.length; i++) // { // for(var j=i+1; j<arr.length; j++) // { // if(arr[i] > arr[j]) // { // temp = arr[i]; // arr[i] = arr[j]; // arr[j] = temp; // } // } // } // for(var x in arr) // { // console.log(arr[x]); // console.log(arr); // } var arr = [12,34,3,24,78,90,16,45], temp = 0; for(var i=0; i<arr.length; i++) { for(var j=arr.length-1; j>=i; j--) { if(arr[j] > arr[j+1]) { temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } } for(var x in arr) { console.log(arr[x]); } </script> <script> var arr1 = [1,3,5,2,11,33,78,55,99,88]; var temp = 0; for(var i = 0; i<arr.length; i++){ for(var j = i+1;j<arr.length;j++){ arr[i] = arr[j]; } } </script> </body> </html>