<!doctype html> <html> <head> <meta charset="UTF-8"> <title>递归算法示例</title> <style type="text/css"> #result{ color:red; //border:1px solid #000080; padding:10px; } #main{ margin:10px; } </style> <script src="jquery-1.12.4.min.js"></script> <script type="text/javascript"> $(function(){ $(‘#btnCalc‘).click(function(){ var num = parseInt($(‘#txtCount‘).val()) var strResult = ‘‘ for(var i=1;i<=num;i++){ var t = new Date() if(i < num){ strResult += String(calc(i)) + ‘ 计算耗时 ‘ + ((new Date()).getTime() - t.getTime())/1000.0 + ‘ 秒<br />‘ } else{ strResult += String(calc(i)) + ‘ 计算耗时 ‘ + ((new Date()).getTime() - t.getTime())/1000.0 + ‘ 秒‘ } } $(‘#result‘).html(strResult) $(‘#result‘).css(‘border‘,‘1px solid #000080‘) }) }) function calc(num){ if(num <= 0){ return 0 } if(num == 1 || result == 2){ return 1 } else { return calc(num-1) + calc(num-2) } } </script> </head> <body> <div id="result"></div> <div id="main"> <input type="button" id="btnCalc" value="计算" /> <input type="text" id="txtCount" value="30" /> </div> </body> </html>
原文:http://www.cnblogs.com/z5337/p/7745722.html