
本文介绍如何在php中从预设数值数组中高效地查找一个目标数的最佳构成要素。针对传统贪婪算法的不足,我们提出一种优化方法,通过遍历所有可能的构成数值,计算其倍数和余数,并利用自定义排序逻辑,优先选择余数最小、其次选择倍数最小的方案,从而实现更精确的近似匹配。
在许多业务场景中,我们可能需要从一组预设的数值(例如商品尺寸、货币面额、资源配额等)中,找出最接近或精确构成某个目标总量的组合。例如,给定一个目标金额3000,以及一组可选的数值:{700, 800, 900, 950, 1000, 1100, 1200, 1300},我们需要找到一个或多个这些数值的组合,使其总和尽可能接近3000,且余数最小。如果存在多种方案余数相同,则通常倾向于选择使用较少构成次数的方案。
一种常见的直观做法是采用贪婪策略:从最大的预设数值开始,尽可能多地使用它,然后用剩余的金额对下一个最大的数值重复此过程。然而,这种方法并非总能得到最优解。
考虑以下PHP代码示例,它展示了这种贪婪策略:
<?php
$amount = 3000;
$sizes = array(1300, 1200, 1100, 1000, 950, 900, 800, 700); // 注意这里已按降序排列
$result = array();
$remaining_amount = $amount; // 使用一个变量来跟踪剩余金额
foreach ($sizes as $size) {
if ($remaining_amount >= $size) {
$times = floor($remaining_amount / $size);
$result[$size] = $times;
$remaining_amount -= $times * $size;
} else {
$result[$size] = 0;
}
}
echo '<pre>'; print_r($result); echo '</pre>';
?>对于目标金额 3000,上述代码的输出将是:
立即学习“PHP免费学习笔记(深入)”;
Array
(
[1300] => 2
[1200] => 0
[1100] => 0
[1000] => 0
// ... 其他为0
)这表示使用了两个 1300,总计 2600,剩余 400。然而,我们知道 1000 * 3 = 3000 是一个更优的解决方案,它能精确构成目标金额,余数为 0。这说明贪婪算法在某些情况下会错过全局最优解。
为了克服贪婪算法的局限性,我们需要一种方法来评估所有可能的“单个构成要素”方案,并从中选出最佳的一个。这里的“单个构成要素”指的是从预设数组中选取一个数值,并重复使用它来逼近目标金额。
核心思想是遍历 sizes 数组中的每一个预设数值,独立地计算它能构成目标金额的次数 (times) 和此时产生的余数 (remainder)。
<?php
$amount = 3000;
$sizes = [ 1300, 1200, 1100, 1000, 950, 900, 800, 700 ]; // 顺序不影响此步骤
$evaluation_results = [];
foreach ($sizes as $size) {
$times = (int)floor($amount / $size); // 计算当前size能构成的次数
$remainder = $amount - $times * $size; // 计算余数
$evaluation_results[] = [
'size' => $size,
'times' => $times,
'remainder' => $remainder
];
}
// 此时 $evaluation_results 包含了所有单一种类构成方案的评估
// ... 接下来进行排序
?>在这一步之后,$evaluation_results 数组将包含类似以下结构的数据:
Array
(
[0] => Array ( [size] => 1300, [times] => 2, [remainder] => 400 )
[1] => Array ( [size] => 1200, [times] => 2, [remainder] => 600 )
[2] => Array ( [size] => 1100, [times] => 2, [remainder] => 800 )
[3] => Array ( [size] => 1000, [times] => 3, [remainder] => 0 ) // 理想方案
// ... 其他
)为了找到“最佳”方案,我们需要对 evaluation_results 数组进行排序。排序的优先级规则如下:
PHP的 usort 函数非常适合这种自定义排序需求。
<?php
// ... (接上一步的代码)
usort($evaluation_results, static function ($item1, $item2): int {
// 首先比较余数,余数小的排在前面
$comparison = $item1['remainder'] <=> $item2['remainder'];
// 如果余数相同,则比较构成次数,次数少的排在前面
return $comparison === 0 ? $item1['times'] <=> $item2['times'] : $comparison;
});
echo '<pre>'; print_r($evaluation_results); echo '</pre>';
?>将上述两个步骤整合,完整的PHP代码如下:
<?php
$amount = 3000; // 目标金额
$sizes = [ 1300, 1200, 1100, 1000, 950, 900, 800, 700 ]; // 预设构成数值
$evaluation_results = [];
// 步骤一:计算所有可能的构成方案
foreach ($sizes as $size) {
$times = (int)floor($amount / $size);
$remainder = $amount - $times * $size;
$evaluation_results[] = [
'size' => $size,
'times' => $times,
'remainder' => $remainder
];
}
// 步骤二:自定义排序以寻找最佳方案
usort($evaluation_results, static function ($item1, $item2): int {
// 优先按余数升序排序
$comparison = $item1['remainder'] <=> $item2['remainder'];
// 如果余数相同,则按构成次数升序排序
return $comparison === 0 ? $item1['times'] <=> $item2['times'] : $comparison;
});
echo '<pre>';
print_r($evaluation_results);
echo '</pre>';
?>运行这段代码,输出将是:
Array
(
[0] => Array
(
[size] => 1000
[times] => 3
[remainder] => 0 // 最佳结果:余数为0
)
[1] => Array
(
[size] => 950
[times] => 3
[remainder] => 150 // 次佳结果
)
[2] => Array
(
[size] => 700
[times] => 4
[remainder] => 200
)
[3] => Array
(
[size] => 900
[times] => 3
[remainder] => 300
)
[4] => Array
(
[size] => 1300
[times] => 2
[remainder] => 400
)
[5] => Array
(
[size] => 1200
[times] => 2 // 余数与下一个相同,但次数更少,故排在前
[remainder] => 600
)
[6] => Array
(
[size] => 800
[times] => 3 // 余数与上一个相同,但次数更多,故排在后
[remainder] => 600
)
[7] => Array
(
[size] => 1100
[times] => 2
[remainder] => 800
)
)通过这个排序后的数组,我们很容易就能找到最优解:数组的第一个元素 [0] 即是最佳方案。对于目标金额 3000,最佳方案是使用 1000 这个数值 3 次,余数为 0。
通过对所有可能的单一构成方案进行全面评估,并结合自定义排序逻辑,我们可以有效地从一组预设数值中找到一个目标数的最佳近似构成。这种方法避免了贪婪算法可能导致的局部最优问题,确保了在给定约束下的最优解。在实际开发中,理解问题的具体需求(是寻找单一构成还是多重组合)是选择合适算法的关键。
以上就是优化PHP算法:从预设值中寻找目标数的最佳近似构成的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号