[英]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现在我有一个代码,它显示了 3 种食物的所有可能结果,唯一的事情是我希望它用我给 readline 提供的预算来做它需要显示这样的东西
• 6 worsten, 100 hamburgers, 75 frikandellen • 6 个最坏的、100 个汉堡包、75 个 frikandellen
• 6 worsten, 160 hamburgers, 25 frikandellen • 6 个最坏的、160 个汉堡包、25 个 frikandellen
• 12 worsten, 30 hamburgers, 100 frikandellen • 12 个最坏的、30 个汉堡包、100 个 frikandellen
price per item每件商品的价格
• Worst ( 6 pack) 5 euro • 最差(6 包)5 欧元
• Hamburgers ( 20 pack) 10 euro • 汉堡包(20 包)10 欧元
• Frikandellen (25 pack) 15 euro • Frikandellen(25 包)15 欧元
my php code我的PHP代码
<?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 个最坏的、100 个汉堡包、75 个 frikandellen
• 6 worsten, 160 hamburgers, 25 frikandellen • 6 个最坏的、160 个汉堡包、25 个 frikandellen
• 12 worsten, 30 hamburgers, 100 frikandellen • 12 个最坏的、30 个汉堡包、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.这是对该主题的一种相当蛮力的方法,并且有相当多的优化潜力,但它应该为您指明正确的方向。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.