Skip to content

Instantly share code, notes, and snippets.

@zonuexe
Last active August 27, 2022 09:53
Show Gist options
  • Save zonuexe/0154d5a458362e44bd4f4cd0ff016b9f to your computer and use it in GitHub Desktop.
Save zonuexe/0154d5a458362e44bd4f4cd0ff016b9f to your computer and use it in GitHub Desktop.
めもりー沖縄
<?php
$money = filter_var($argv[1] ?? 12345, FILTER_VALIDATE_INT);
$coins = [10000, 5000, 2000, 1000, 500, 100, 50, 10, 5, 1];
$charge = [];
foreach ($coins as $coin) {
array_push($charge, ...array_fill(0, intdiv($money, $coin), $coin));
$money %= $coin;
}
echo json_encode($charge);
<?=json_encode(array_reduce([1e4,5e3,2e3,1e3,500,100,50,10,5,1],fn($x,$y)=>[$x[0]%$y,[...$x[1],...@array_fill(0,$x[0]/$y,$y)]],[12345,[]])[1]);
<?=json_encode(array_reduce([10000,5000,2000,1000,500,100,50,10,5,1],fn($x,$y)=>[$x[0]%$y,[...$x[1],...array_fill(0,$x[0]/$y,$y)]],[12345,[]])[1])?>
<?= json_encode(array_reduce(
[10000, 5000, 2000, 1000, 500, 100, 50, 10, 5, 1],
fn($carry, $coin) => [
$carry[0] % $coin,
[...$carry[1], ...array_fill(0, intdiv($carry[0], $coin), $coin)]
],
[12345, []]
)[1]) ?>
<?= json_encode(array_reduce(
[10000, 5000, 2000, 1000, 500, 100, 50, 10, 5, 1],
fn($carry, $coin) => [
gmp_mod($carry[0], $coin),
[...$carry[1], ...array_fill(0, (string)gmp_div($carry[0], $coin), $coin)]
],
[12345, []]
)[1]) ?>
<?php
$coins = [10000, 5000, 2000, 1000, 500, 100, 50, 10, 5, 1];
$money = 11290;
$charge = [];
$f = function ($coins, $charge, $money) use (&$f) {
$coin = array_shift($coins);
if ($coin <= 0) {
return $charge;
}
$charge = [...$charge, ...array_fill(0, intdiv($money, $coin), $coin)];
$money = $money % $coin;
return $f($coins, $charge, $money);
};
echo json_encode($f($coins, [], 12345));
<?php
$coins = [100_000_000_000_000_000, 10_000_000_000_000, 10_000_000_000, 10_000_000, 5000, 2000, 1000, 500, 100, 50, 10, 5, 1];
$charge = [];
$f = function ($coins, $charge, $money) use (&$f) {
$coin = array_shift($coins);
if ($coin <= 0) {
return;
}
$div = gmp_div_q($money, $coin);
while ($div >= 0) {
$div -= gmp_sub($div, 1);
echo "$coin, ";
}
$money = gmp_mod($money, $coin);
return $f($coins, $charge, $money);
};
echo "[";
$f($coins, [], (string)gmp_add(PHP_INT_MAX, 1));
echo "]";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment