June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
30
Task/Count-the-coins/Perl-6/count-the-coins.pl6
Normal file
30
Task/Count-the-coins/Perl-6/count-the-coins.pl6
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
# Recursive (cached)
|
||||
sub change-r($amount, @coins) {
|
||||
my @cache = $[1 xx @coins];
|
||||
|
||||
multi ways($n where $n >= 0, @now [$coin,*@later]) {
|
||||
@cache[$n][+@later] //= ways($n - $coin, @now) + ways($n, @later);
|
||||
}
|
||||
multi ways($,@) { 0 }
|
||||
|
||||
# more efficient to start with coins sorted in descending order
|
||||
ways($amount, @coins.sort(-*).list);
|
||||
}
|
||||
|
||||
change-r 1_00, [1,5,10,25];
|
||||
change-r 1000_00, [1,5,10,25,50,100];
|
||||
|
||||
# Iterative
|
||||
sub change-i(\n, @coins) {
|
||||
my @table = [1 xx @coins], [0 xx @coins] xx n;
|
||||
for 1..n X ^@coins -> (\i, \j) {
|
||||
my \c = @coins[j];
|
||||
@table[i][j] = [+]
|
||||
@table[i - c][j ] // 0,
|
||||
@table[i ][j - 1] // 0;
|
||||
}
|
||||
@table[*-1][*-1];
|
||||
}
|
||||
|
||||
say change-i 1_00, [1,5,10,25];
|
||||
say change-i 1000_00, [1,5,10,25,50,100];
|
||||
|
|
@ -12,5 +12,5 @@ fn make_change(coins: &[usize], cents: usize) -> usize {
|
|||
|
||||
fn main() {
|
||||
println!("{}", make_change(&[1,5,10,25], 100));
|
||||
println!("{}", make_change(&[1,5,10,25,50,100], 100000));
|
||||
println!("{}", make_change(&[1,5,10,25,50,100], 100_000));
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue