我需要编写一个 PHP 脚本来计算除以数字和提醒的时间数。可以说$amount=9200;
,如果我将其除以5000 ,那么输出应该是5000: 1 times
and Reminder: 4200
。我想我需要使用$n=$amount%5000;
但我只有提醒而不是否。它分裂的次数。
谢谢!!
这就是众所周知的欧几里得划分: http ://en.wikipedia.org/wiki/Euclidean_division
$amount = 9200;
$divide = 5000;
$times = floor($amount/$divide);
$reminder = $amount%$divide;
echo "$amount = $times times $divide plus $reminder";
%
操作员会给你余数,然后你需要做另一个操作来得到你划分的次数。
$times = floor($amount/5000);
$times = floor($amount / 5000);
$remainder = $amount % 5000;
<?php
class ATM
{
public function deliver( $note )
{
// code to grab that not from the cash boxes...
}
}
$notes = new SplFixedArray(5);
$notes[0] = 100;
$notes[1] = 50;
$notes[2] = 20;
$notes[3] = 10;
$notes[4] = 5;
$notesKey = 0;
$withdraw = 920;
$allocated = 0;
$deliver = new SplQueue();
// work out biggest notes for remaining value and queue
while($allocated < $withdraw)
{
$remains = ($withdraw-$allocated) % $notes[$notesKey];
$numNotes = (($withdraw-$allocated)-$remains)/$notes[$notesKey];
for( $i = 0; $i < $numNotes; $i++ )
{
$allocated += $notes[$notesKey];
$deliver->enqueue($notesKey);
}
++$notesKey;
}
$atm = new ATM();
while(!$deliver->isempty())
{
$atm->deliver($notes[$deliver->dequeue()]);
}
?>
像这样的东西应该可以解决问题......