Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
19
Task/Count-the-coins/JavaScript/count-the-coins-1.js
Normal file
19
Task/Count-the-coins/JavaScript/count-the-coins-1.js
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
function countcoins(t, o) {
|
||||
'use strict';
|
||||
var targetsLength = t + 1;
|
||||
var operandsLength = o.length;
|
||||
t = [1];
|
||||
|
||||
for (var a = 0; a < operandsLength; a++) {
|
||||
for (var b = 1; b < targetsLength; b++) {
|
||||
|
||||
// initialise undefined target
|
||||
t[b] = t[b] ? t[b] : 0;
|
||||
|
||||
// accumulate target + operand ways
|
||||
t[b] += (b < o[a]) ? 0 : t[b - o[a]];
|
||||
}
|
||||
}
|
||||
|
||||
return t[targetsLength - 1];
|
||||
}
|
||||
2
Task/Count-the-coins/JavaScript/count-the-coins-2.js
Normal file
2
Task/Count-the-coins/JavaScript/count-the-coins-2.js
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
countcoins(100, [1,5,10,25]);
|
||||
242
|
||||
25
Task/Count-the-coins/JavaScript/count-the-coins-3.js
Normal file
25
Task/Count-the-coins/JavaScript/count-the-coins-3.js
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
function countcoins(t, o) {
|
||||
'use strict';
|
||||
var operandsLength = o.length;
|
||||
var solutions = 0;
|
||||
|
||||
function permutate(a, x) {
|
||||
|
||||
// base case
|
||||
if (a === t) {
|
||||
solutions++;
|
||||
}
|
||||
|
||||
// recursive case
|
||||
else if (a < t) {
|
||||
for (var i = 0; i < operandsLength; i++) {
|
||||
if (i >= x) {
|
||||
permutate(o[i] + a, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
permutate(0, 0);
|
||||
return solutions;
|
||||
}
|
||||
2
Task/Count-the-coins/JavaScript/count-the-coins-4.js
Normal file
2
Task/Count-the-coins/JavaScript/count-the-coins-4.js
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
countcoins(100, [1,5,10,25]);
|
||||
242
|
||||
8
Task/Count-the-coins/JavaScript/count-the-coins-5.js
Normal file
8
Task/Count-the-coins/JavaScript/count-the-coins-5.js
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
var amount = 100,
|
||||
coin = [1, 5, 10, 25]
|
||||
var t = [1];
|
||||
for (t[amount] = 0, a = 1; a < amount; a++) t[a] = 0 // initialise t[0..amount]=[1,0,...,0]
|
||||
for (var i = 0, e = coin.length; i < e; i++)
|
||||
for (var ci = coin[i], a = ci; a <= amount; a++)
|
||||
t[a] += t[a - ci]
|
||||
document.write(t[amount])
|
||||
Loading…
Add table
Add a link
Reference in a new issue