-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest1.js
More file actions
59 lines (36 loc) · 1.16 KB
/
test1.js
File metadata and controls
59 lines (36 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
(function () {
var sum = 200;
//change this value to test
var coinTypes = [1, 2, 5, 10, 20, 50, 100, 200];
var chainBuffer = [];
//this variable contain the permutation result.
function CoinSums(b, sum) {
if(!b) var b = [];
//b means buffer
if (sum === 0) {
//indicate the termination of loop (permutation)
chainBuffer.push(b);
return;
}
for (var c = 0, l = coinTypes.length; c < l; c++) {
var a = b.slice();
//save the sate of incoming query
if(coinTypes[c] <= sum && (!b.length || coinTypes[c] >= b[b.length-1])) {
b.push(coinTypes[c]);
CoinSums(b, sum - coinTypes[c]);
/* if you find a value less than sum, means that you have at least so many permutations,
while you take out value coinTypes[c], the remainder has its own permutation, the samllest coin has value 1,
do not worry about the remainder can not build a permutation
(!b.length || coinTypes[c] >= b[b.length-1]) control the order be consecutive
*/
}
b = a;
//restore incoming query status
}
}
CoinSums(null, sum);
var result = chainBuffer.length;
window.onload=function(){document.body.innerHTML = '<pre>there are ' + result + ' possible combinations.</pre>';}
//console.log(result);
//73682
})();