Last active
June 6, 2017 10:10
-
-
Save xupengzhuo/724e421471e7c51fd10c2ff8ed63e53d to your computer and use it in GitHub Desktop.
给定一个数,获取和为这个数的组合并输出
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!doctype HTML> | |
| <head> | |
| <style> | |
| .green { | |
| color: green; | |
| } | |
| </style> | |
| </head> | |
| <body> | |
| <div id='numbers'></div> | |
| </body> | |
| <script> | |
| // Math.floor(Math.random() * (max - min)) + min; | |
| var get_ratio = function (num) { | |
| var i = 0, | |
| j = num; | |
| var l = []; | |
| while (i < (num - 1)) { | |
| l.push(Math.random()) | |
| i++; | |
| } | |
| l = l.concat([0, 1]).sort() | |
| // console.log(l); | |
| var d = []; | |
| while (j >= 0) { | |
| if (!isNaN(l[j - 1])) { | |
| d.push(l[j] - l[j - 1]) | |
| } | |
| j-- | |
| } | |
| return d | |
| } | |
| var get_piece = function (cake, rates) { | |
| return rates.map(function (r) { | |
| return (r * cake) | |
| }) | |
| } | |
| var condition = function (pieces, min, max) { | |
| for (var i in pieces) { | |
| console.log(pieces[i], typeof pieces[i], min, max) | |
| if (pieces[i] < min || pieces[i] > max) { | |
| return false | |
| } | |
| } | |
| return true; | |
| } | |
| var i = 1000000, pieces = []; | |
| while (i > 0) { | |
| i--; | |
| pieces = get_piece(117239.42, get_ratio(13)) | |
| var isvalid = condition(pieces, 5000, 15000); | |
| if (isvalid) { | |
| document.write('<div class="green">' + pieces.map(a => a.toFixed(2)).join(', ') + '</div>') | |
| document.write('<br/>') | |
| } else { | |
| // document.write('<div >' + pieces.map(a => a.toFixed(2)).join(', ') + '</div>') | |
| // document.write('<br/>') | |
| } | |
| } | |
| alert('done') | |
| </script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment