Choosing coins with least or no change given

前端 未结 8 1383
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-07 04:00

I am making a game which consists of coin denominations of $10, $5, $3, and $1. The player may have 0 or more of each type of currency in his inventory with a maximum of 15 coin

8条回答
  •  甜味超标
    2021-02-07 04:26

    The solution I was able to made covers the 3 examples posted in your question. And always gives the change with as few coins as possible.

    The tests I made seemed to be executed very fast.

    Here I post the code:

    = 0; $i--) {
            if (check_availability($i)) {
                return $i;
            }
        }
        return -1;
    }
    
    for($i=0;$i<4;$i++) {
        while(check_availability($i) && ($btotal + $coin_value[$i]) <= $price) {
            $btotal += $coin_value[$i];
            $barray[$i]++;
        }
    }
    
    if($price != $btotal) {
        $buf = check_lower_available();
        for ($i = $buf; $i >= 0; $i--) {
            if (check_availability($i) && ($btotal + $coin_value[$i]) > $price) {
                $btotal += $coin_value[$i];
                $barray[$i]++;
                break;
            }
        }
    }
    
    // Time to pay
    $bchange = 0;
    $barray_change = array(0,0,0,0);
    
    if ($price > $btotal) {
        echo "You have not enough money.";
    }
    else {
        $pay_msg = "You paid $".$btotal."\n\n";
        $pay_msg.= "You used ".$barray[0]." coins of $10\n";
        $pay_msg.= "You used ".$barray[1]." coins of $5\n";
        $pay_msg.= "You used ".$barray[2]." coins of $3\n";
        $pay_msg.= "You used ".$barray[3]." coins of $1\n\n\n";
        // Time to give change
        $the_diff = $btotal - $price;
        if (!empty($the_diff)) {
            for ($i = 0; $i < 4; $i++) {
                while($the_diff >= $coin_value[$i]) {
                    $bchange += $coin_value[$i];
                    $barray_change[$i]++;
                    $the_diff -= $coin_value[$i];
                }
            }
    
            $check_sum = array_sum($inventory) - array_sum($barray);
            $check_sum+= array_sum($barray_change);
            $msg = "";
            if ($check_sum < 15) {
                $change_msg = "Your change: $".$bchange."\n\n";
                $change_msg.= "You received ".$barray_change[0]." coins of $10\n";
                $change_msg.= "You received ".$barray_change[1]." coins of $5\n";
                $change_msg.= "You received ".$barray_change[2]." coins of $3\n";
                $change_msg.= "You received ".$barray_change[3]." coins of $1\n\n";
                $msg = $pay_msg.$change_msg;
            }
            else {
                $msg = "You have not enough space to hold the change.\n";
                $msg.= "Buy cancelled.\n";
            }
        }
        else {
            $msg = $pay_msg."You do not need change\n";
        }
        if ($check_sum < 15) {
            for ($i = 0; $i < 4; $i++) {
                $inventory[$i] -= $barray[$i];
                $total_coins-= $barray[$i];
            }
            for ($i = 0; $i < 4; $i++) {
                $inventory[$i] += $barray_change[$i];
                $total_coins+= $barray[$i];
            }
        }
        echo $msg;
        echo "Now you have:\n";
        echo $inventory[0]." coins of $10\n";
        echo $inventory[1]." coins of $5\n";
        echo $inventory[2]." coins of $3\n";
        echo $inventory[3]." coins of $1\n";
    }
    

提交回复
热议问题