简体   繁体   中英

showing all possible options with a budget for food items in php

right now I have a code that shows all the possible outcomes with 3 food items only thing is that I want it to do it with a budget that I give with readline it needs to display something like this

• 6 worsten, 100 hamburgers, 75 frikandellen

• 6 worsten, 160 hamburgers, 25 frikandellen

• 12 worsten, 30 hamburgers, 100 frikandellen

price per item

• Worst ( 6 pack) 5 euro

• Hamburgers ( 20 pack) 10 euro

• Frikandellen (25 pack) 15 euro

my php code

<?php
function f_($n) {
    if($n<2) { return 1; }
    for($x = 2;$n-1>1;$x*=$n--);
    return $x;
}


function array_restore($r) {
    $clean = array();
    if(is_array($r)) {
        foreach($r as $k) {
            $clean[] = $k;
        }
    }

    return $clean;
}

function connect($arr){
    $back = "";
    foreach($arr as $a){
        if($back != ""){
            $back .= " ";
        }

        $back .= $a;
    }
    return $back;
}

function cmb($v) {
    $str = count($v);
    $tot = f_($str);

    $combo = array();
    for($i=0;$i<$tot*8;$i++) {
        shuffle($v);
        $sf = connect($v);
        if(!in_array($sf, $combo)){
            $combo[] = $sf;
        }
    }

    $x = array_unique($combo);
    return array_restore($x);
}

var_dump(cmb(array("frikandel"=>15 , "worst"=>5, "hamburger"=>10)));
function pc_permute($items, $perms = array( )) {
    $back = array();
    if (empty($items)) {
        $back[] = join(' ', $perms);
    } else {
        for ($i = count($items) - 1; $i >= 0; --$i) {
            $newitems = $items;
            $newperms = $perms;
            list($foo) = array_splice($newitems, $i, 1);
            array_unshift($newperms, $foo);
            $back = array_merge($back, pc_permute($newitems, $newperms));
        }
    }
    return $back;
}





right now I get this as my output right now I get this

[0]=>
  string(7) "15 5 10"
  [1]=>
  string(7) "5 10 15"
  [2]=>
  string(7) "15 10 5"
  [3]=>
  string(7) "10 5 15"
  [4]=>
  string(7) "10 15 5"
  [5]=>
  string(7) "5 15 10"

instead I need to get something like this

• 6 worsten, 100 hamburgers, 75 frikandellen

• 6 worsten, 160 hamburgers, 25 frikandellen

• 12 worsten, 30 hamburgers, 100 frikandellen

This is what I came up with to solve your problem (my apologies, but it has nothing in common with your code).

<?php
    $budget = 200;
    
    //The idea is, to iterate over each food combination, until the budget is cracked.
    //The loop stops, after the last food item exceeds the limit all by itself.

    $counts  = [ 0, 0, 0 ];

    while (true)
    {
        $total =    
            $counts[0]      * 5         //Worst
            + $counts[1]    * 10        //Hamburger
            + $counts[2]    * 15;       //Frikandellen

        if ($total < $budget - 5)       //Still room left for one more in the budget.
            ++$counts[0];
        else if (!$counts[0])           //The total was exceeded with the current combination without adding a single worst.
        {
            if (!$counts[1])            //The final iteration has been reached.
                break;
            else
            {
                ++$counts[2];
                $counts[1] = 0;
            }
        }
        else
        {
            echo sprintf("%d worsten, %d hamburgers, %d frikandellen\n",
                $counts[0] * 6,
                $counts[1] * 20,
                $counts[2] * 25
            );

            ++$counts[1];               //Increment the next place.
            $counts[0] = 0;             //And reset the previous iteration.
        }
    }
?>

It's a rather brute-force approach to the topic and there's quite a bit of optimization potential, but it should point you in the right direction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM