在 PHP 中允许使用可用的国家/地区面额检查最终金额

需要制作自定义功能来检查可用面额的金额。我做的代码:


$amount = 100;

$notes_aval = array(20,50,100,500,2000);//available currency notes

    $is_allowed = 0; //not allowed

    foreach($notes_aval as $note){

         if (fmod($amount,$note) == 0) {

         $is_allowed = 1;//allowed

          }

       }

echo $is_allowed;

但这并不适用于所有情况。考试:我有面额 = array (20,50); 数量为 90 是不允许的,但应该允许 20*2 + 50*1 = 90


在面额 = array (20,50) 的示例中,如果金额 1110 应该可以接受,则 1110 = 20*53 + 50*1


缥缈止盈
浏览 131回答 2
2回答

胡说叔叔

您需要从最大的值开始兑换,直到您的金额小于最大的纸币(例如 2000)。然后你用更低的音符(例如500)做同样的事情,然后再用更低的音符。当金额小于最低值(例如 20)时,您无法兑换此金额。所以:我们从2270开始我们检查最大的纸币 - 它是 2000。现在我们知道我们有 2000 和 270 (2270 - 2000) 休息现在我们再次检查最大值 - 它是 200所以我们有 2000、200 和 70 (270 - 200) 休息现在最大的不可能是 50所以我们有 2000, 200, 50 和 20 (70 - 50) 休息现在最大的是 20,我们有 2000、200、50、20,其余的是 0由于休息比最低音符小,所以我们可以停止检查。如果 rest 为 0,我们知道我们可以交换,如果 rest 大于 0,则我们不能。此外,我们还有可用于交换的票据列表(2000、200、50、20)。function checkDenomination($amount){    $notes = array(2000,500,100,50,20); //it's easier if they are reversed    $smallestNote = 20;    $result = [];    while($amount >= $smallestNote) { //we will repeat until we can exchange        foreach($notes as $note) {             if ($amount >= $note) { //we check for largest value we can exchange                $result[] = $note;                $amount -= $note; //as we have hit, we can deduct it from amount;                break;            }        }    }    return ($amount > 0) ? false : $result; //return false if we cannot exchange this amount or array with notes we can exchange for full amount}var_dump(checkDenomination(100));var_dump(checkDenomination(23424));var_dump(checkDenomination(25000));var_dump(checkDenomination(222));

猛跑小猪

尝试两个模块化部门function validateCurrency($amount){&nbsp; &nbsp; $requestdAmount = $amount;&nbsp; &nbsp; $valueUnder = 0;&nbsp; &nbsp; $notes = array(20, 50,100,500,2000);&nbsp;&nbsp; &nbsp; $is_allowed = 0;&nbsp; &nbsp; if(in_array($amount, $notes)){&nbsp; &nbsp; &nbsp; &nbsp; return $is_allowed = 1;&nbsp; &nbsp; }&nbsp; &nbsp; $numOccurance = ceil($amount/$notes[0]);&nbsp; &nbsp; $arraySums = [];&nbsp; &nbsp; foreach ($notes as $key => $value) {&nbsp; &nbsp; for ($i=1; $i <= $numOccurance; $i++) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($value * $i == $amount) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $is_allowed = 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $arraySums[$key][] = $value * $i;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; for ($i=0; $i < count($arraySums); $i++) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; for ($j=$i+1; $j < count($arraySums); $j++) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach ($arraySums[$i] as $key => $value) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach ($arraySums[$j] as $key2 => $toBeMul) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($value+$toBeMul == $amount) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $is_allowed = 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return $is_allowed;}// Driver Code&nbsp;$amount = 40;&nbsp;$is_allowed =&nbsp; validateCurrency($amount);&nbsp;echo $is_allowed;die();它会工作
打开App,查看更多内容
随时随地看视频慕课网APP