JavaScript黑洞数字之运算路线查找算法(递归算法)实例
本文实例讲述了JavaScript黑洞数字之运算路线查找算法。分享给大家供大家参考,具体如下:
运行效果截图如下:
具体代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>运算路线查找算法</title> <script type="text/javascript" > var BLACKHOLENMB = 6174; var count = 0; function blackHole(nmb) { if (isNaN(nmb) || nmb < 1000 || nmb > 9999) { return -1; } count++; var tempArray = (nmb + "").split(""); var smallNmb = parseInt(tempArray.sort().join("")); var bigNmb = parseInt(tempArray.reverse().join("")); var d_value = bigNmb - smallNmb; log(bigNmb, smallNmb, d_value, count); if (d_value != BLACKHOLENMB) { return blackHole(d_value); } else { return count; } } function log(big, small, d_value, count) { console.log("step " + count + ":" + big + "-" + small + "=" + d_value); } console.log(blackHole(2167)); </script> </head> <body> </body> </html>
更多关于JavaScript算法相关内容感兴趣的读者可查看本站专题:《JavaScript排序算法总结》、《JavaScript遍历算法与技巧总结》及《JavaScript数据结构与算法技巧总结》
希望本文所述对大家JavaScript程序设计有所帮助。
相关推荐
steeven 2020-11-10
Tips 2020-10-14
nongfusanquan0 2020-08-18
yedaoxiaodi 2020-07-26
清溪算法君老号 2020-06-27
pengkingli 2020-06-25
yishujixiaoxiao 2020-06-25
清溪算法 2020-06-21
RememberMePlease 2020-06-17
nurvnurv 2020-06-05
SystemArchitect 2020-06-02
码墨 2020-05-29
清溪算法 2020-05-27
choupiaoyi 2020-05-27
清溪算法 2020-05-25
bluewelkin 2020-05-19
dbhllnr 2020-05-15
steeven 2020-05-09
baike 2020-05-09