Anonymous user
Count the coins: Difference between revisions
Updated D code
(Updated Python code) |
(Updated D code) |
||
Line 161:
}
BigInt
assert(c > 0);
static int findCycle(in Cents amount, in int[] coins)
pure nothrow {
int lcycle;
return lcycle * coins.length;
}
immutable int cycle = findCycle(amount, coins);
▲ out(result) {
immutable size_t n = coins.length;
▲ assert(cast()result > 0); // de-const, bug workaround
}▼
▲ body {
▲ immutable int n = coins.length;
t[0 .. n] = BigInt(1);▼
auto table = new
▲ if (coins[0] <= s)
int
int
for (int s = 1; s <=
if (i == 0 && pos
▲ t[pos] += t[pos - 1];
if (coins[i] <= s)
table[pos] = (q >= 0) ? table[q] : table[q + cycle];
}
table[pos] += table[pos - 1];
i++;
if (i == n) {
s++;
}
pos++;
}
return table[pos - 1];
}
void main() {
immutable usCoins = [100, 50, 25, 10, 5, 1];
writeln(changeCount(Cents( 1_00), usCoins[2 .. $]));▼
writeln(changeCount(Cents( 1000_00), usCoins));▼
writeln(changeCount(Cents(10000_00), usCoins));▼
immutable euCoins = [200, 100, 50, 20, 10, 5, 2, 1];
writeln(countChanges(Cents(100000_00), coins), "\n");
▲ }
}</lang>
Output:
Line 205 ⟶ 224:
13398445413854501
1333983445341383545001
133339833445334138335450001
4562
10056050940818192726001
99341140660285639188927260001
992198221207406412424859964272600001</pre>
=={{header|Factor}}==
|