
云EC电商系统(简称云EC)是由佛山市云迈电子商务有限公司自主开发的一套免费、开源的基于PHP+MYSQL电商系统软件。 云EC电商系统 1.2.1 更新日志:2018-08-10 1.修复部分环境下二维码不显示的问题; 2.商品列表增加多属性筛选支持; 3.修复更新优惠券状态时错将已使用的优惠券也更新为过期; 4.修复文章发布远程图片下载失败; 5.修复某些情况下运费计算出错导致
2595
子集和问题是计算机科学和动态规划中的一个经典问题。给定一组正整数和一个目标和,任务是确定是否存在给定集合的子集,其元素之和等于目标和。
<?php
// A recursive solution for the subset sum problem
// Returns true if there is a subset of the set
// with a sum equal to the given sum
function isSubsetSum($set, $n, $sum)
{
// Base Cases
if ($sum == 0)
return true;
if ($n == 0 && $sum != 0)
return false;
// If the last element is greater than the sum, then ignore it
if ($set[$n - 1] > $sum)
return isSubsetSum($set, $n - 1, $sum);
// Check if the sum can be obtained by either including or excluding the last element
return isSubsetSum($set, $n - 1, $sum) ||
isSubsetSum($set, $n - 1, $sum - $set[$n - 1]);
}
// Driver Code
$set = array(1, 7, 4, 9, 2);
$sum = 16;
$n = count($set);
if (isSubsetSum($set, $n, $sum) == true)
echo "Found a subset with the given sum<br>";
else
echo "No subset with the given sum<br>";
$sum = 25;
$n = count($set);
if (isSubsetSum($set, $n, $sum) == true)
echo "Found a subset with the given sum.";
else
echo "No subset with the given sum.";
?>
Found a subset with the given sum. No subset with the given sum.
在提供的示例中,集合为 [1, 7, 4, 9, 2],目标和为 16 和 25。目标和为 25 的第二次调用返回 false,表示不存在子集加起来为 25。因此输出为 Found a subset with the给定 sum in first call。第二次调用中没有给定总和的子集。
<?php
// A Dynamic Programming solution for
// subset sum problem
// Returns true if there is a subset of
// set[] with sun equal to given sum
function isSubsetSum( $set, $n, $sum)
{
// The value of subset[i][j] will
// be true if there is a subset of
// set[0..j-1] with sum equal to i
$subset = array(array());
// If sum is 0, then answer is true
for ( $i = 0; $i <= $n; $i++)
$subset[$i][0] = true;
// If sum is not 0 and set is empty,
// then answer is false
for ( $i = 1; $i <= $sum; $i++)
$subset[0][$i] = false;
// Fill the subset table in bottom
// up manner
for ($i = 1; $i <= $n; $i++)
{
for ($j = 1; $j <= $sum; $j++)
{
if($j < $set[$i-1])
$subset[$i][$j] =
$subset[$i-1][$j];
if ($j >= $set[$i-1])
$subset[$i][$j] =
$subset[$i-1][$j] ||
$subset[$i - 1][$j -
$set[$i-1]];
}
}
/* // uncomment this code to print table
for (int i = 0; i <= n; i++)
{
for (int j = 0; j <= sum; j++)
printf ("%4d", subset[i][j]);
printf("n");
}*/
return $subset[$n][$sum];
}
// Driver program to test above function
$set = array(8,15,26,35,42,59);
$sum = 50;
$n = count($set);
if (isSubsetSum($set, $n, $sum) == true)
echo "Found a subset with given sum.";
else
echo "No subset with given sum.";
?>
Found a subset with given sum.
在提供的示例中,集合为 [8, 15, 26, 35, 42, 59],目标总和为 50。函数调用 isSubsetSum($set, $n, $sum) 返回 true,表示集合中存在一个子集 [8, 42],其加起来等于目标总和 50。因此,代码将找到具有给定总和的子集。
总之,有两种不同的方法来解决子集和问题。第一个解决方案是递归方法,检查给定集合中是否存在总和等于目标总和的子集。它利用回溯来探索所有可能的组合。然而,该解决方案在最坏的情况下可能具有指数时间复杂度。
第二种解决方案利用动态规划并以自下而上的方式解决子集和问题。它构造一个表来存储中间结果,并有效地确定是否存在具有给定总和的子集。这种方法的时间复杂度为 O(n*sum),比递归解决方案更有效。这两种方法都可以用来解决子集和问题,动态规划解决方案对于较大的输入更有效。
以上就是PHP程序用于子集和问题的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号