Posted in Javascript onJanuary 28, 2016
本文实例讲述了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黑洞数字之运算路线查找算法(递归算法)实例
- Author -
m1870164声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@