Php 给定数组中r元素的所有组合
给定如下所示的数组Php 给定数组中r元素的所有组合,php,arrays,combinations,Php,Arrays,Combinations,给定如下所示的数组 $array = ('1', '2', '3', '4', '5', '6', '7'); 我正在寻找一种方法来生成所有可能的组合,每个组合中需要最少数量的元素。(例如,如果r=5,那么它将返回至少包含5个元素的所有可能组合)A组合可以表示为 nCr=n!/(r!-(n-r)!) 首先,我们确定$n为数组中的元素数。$r是每个组合中的最小元素数 $a = ['1', '2', '3', '4', '5', '6', '7']; // the array of elemen
$array = ('1', '2', '3', '4', '5', '6', '7');
我正在寻找一种方法来生成所有可能的组合,每个组合中需要最少数量的元素。(例如,如果r=5,那么它将返回至少包含5个元素的所有可能组合)A组合可以表示为 nCr=n!/(r!-(n-r)!) 首先,我们确定
$n
为数组中的元素数。$r
是每个组合中的最小元素数
$a = ['1', '2', '3', '4', '5', '6', '7']; // the array of elements we are interested in
// Determine the `n` and `r` in nCr = n! / (r! * (n-r)!)
$r = 5;
$n = count($a);
接下来,我们确定$max
为可由$n
二进制数字表示的最大数字。也就是说,如果$n=3
,则$max=(111)
2=7
。为此,我们首先创建一个空字符串$maxBinary
,并向其中添加$n
个1
s。然后我们将其转换为十进制,并将其存储在$max
中
$maxBinary = "";
for ($i = 0; $i < $n; $i++)
{
$maxBinary .= "1";
}
$max = bindec($maxBinary); // convert it into a decimal value, so that we can use it in the following for loop
然后,我们使用与上面相同的技巧为组合选择元素。我相信这些评论足以说明问题
$combs = array(); // the array for all the combinations.
$row = array(); // the array of binary digits in one element of the $allBinary array.
foreach ($allBinary as $key => $one)
{
$combs[$key] = "";
$row = str_split($one); // we store the digits of the binary number individually
foreach ($row as $indx => $digit)
{
if ($digit == '1') // if the digit is 1, then the corresponding element in the array is part of this combination.
{
$combs[$key] .= $a[$indx]; // add the array element at the corresponding index to the combination
}
}
}
就这样。你完了
现在如果你有
echo count($combs);
然后它将为您提供29
补充说明:
我是在看到你的问题后才读到这一点的,作为一名新来者,我发现以下几点很有用:
- 维基百科-
$n
为数组中的元素数。$r
是每个组合中的最小元素数
$a = ['1', '2', '3', '4', '5', '6', '7']; // the array of elements we are interested in
// Determine the `n` and `r` in nCr = n! / (r! * (n-r)!)
$r = 5;
$n = count($a);
接下来,我们确定$max
为可由$n
二进制数字表示的最大数字。也就是说,如果$n=3
,则$max=(111)
2=7
。为此,我们首先创建一个空字符串$maxBinary
,并向其中添加$n
个1
s。然后我们将其转换为十进制,并将其存储在$max
中
$maxBinary = "";
for ($i = 0; $i < $n; $i++)
{
$maxBinary .= "1";
}
$max = bindec($maxBinary); // convert it into a decimal value, so that we can use it in the following for loop
然后,我们使用与上面相同的技巧为组合选择元素。我相信这些评论足以说明问题
$combs = array(); // the array for all the combinations.
$row = array(); // the array of binary digits in one element of the $allBinary array.
foreach ($allBinary as $key => $one)
{
$combs[$key] = "";
$row = str_split($one); // we store the digits of the binary number individually
foreach ($row as $indx => $digit)
{
if ($digit == '1') // if the digit is 1, then the corresponding element in the array is part of this combination.
{
$combs[$key] .= $a[$indx]; // add the array element at the corresponding index to the combination
}
}
}
就这样。你完了
现在如果你有
echo count($combs);
然后它将为您提供29
补充说明:
我是在看到你的问题后才读到这一点的,作为一名新来者,我发现以下几点很有用:
- 维基百科-
n
项中k
项的组合:
function combinationsOf($k, $xs){
if ($k === 0)
return array(array());
if (count($xs) === 0)
return array();
$x = $xs[0];
$xs1 = array_slice($xs,1,count($xs)-1);
$res1 = combinationsOf($k-1,$xs1);
for ($i = 0; $i < count($res1); $i++) {
array_splice($res1[$i], 0, 0, $x);
}
$res2 = combinationsOf($k,$xs1);
return array_merge($res1, $res2);
}
可以使用以下函数递归定义
n
项中k
项的组合:
function combinationsOf($k, $xs){
if ($k === 0)
return array(array());
if (count($xs) === 0)
return array();
$x = $xs[0];
$xs1 = array_slice($xs,1,count($xs)-1);
$res1 = combinationsOf($k-1,$xs1);
for ($i = 0; $i < count($res1); $i++) {
array_splice($res1[$i], 0, 0, $x);
}
$res2 = combinationsOf($k,$xs1);
return array_merge($res1, $res2);
}
函数arrToBit(数组$element){
$bit='';
foreach($元素为$e){
$bit.='1';
}
$length=计数($element);
$num=bindec($bit);
$back=[];
while($num){
$back[]=str_pad(decbin($num),$length,'0',str_pad_LEFT);
$num--;
}
//$back[]=str_pad(decbin(0),$length,'0',str_pad_LEFT);
退还$back;
}
函数bitToArr(数组$element,$bit){
$num=计数($element);
$back=[];
对于($i=0;$i<$num;$i++){
if(substr($bit,$i,1)='1'){
$back[]=$element[$i];
}
}
退还$back;
}
$tags=['a','b','c'];
$bits=arrToBit($tags);
$组合=[];
foreach(比特为$b){
$composition[]=bitToArr($tags,$b);
}
var_dump($组合);
函数arrToBit(数组$element){
$bit='';
foreach($元素为$e){
$bit.='1';
}
$length=计数($element);
$num=bindec($bit);
$back=[];
while($num){
$back[]=str_pad(decbin($num),$length,'0',str_pad_LEFT);
$num--;
}
//$back[]=str_pad(decbin(0),$length,'0',str_pad_LEFT);
退还$back;
}
函数bitToArr(数组$element,$bit){
$num=计数($element);
$back=[];
对于($i=0;$i<$num;$i++){
if(substr($bit,$i,1)='1'){
$back[]=$element[$i];
}
}
退还$back;
}
$tags=['a','b','c'];
$bits=arrToBit($tags);
$组合=[];
foreach(比特为$b){
$composition[]=bitToArr($tags,$b);
}
var_dump($组合);
我用了大约50行代码,得到了29种可能的组合。考虑到n=7
(数组中的元素数)和r=5
,我认为这是正确的答案。所以我们有7C5+7C6+7C7=29
。这就是你所期望的吗?这似乎是正确的,你能发布你的代码吗?我用了大约50行代码,得到了29种可能的组合。考虑到n=7
(数组中的元素数)和r=5
,我认为这是正确的答案。所以我们有7C5+7C6+7C7=29
。这就是你所期望的吗?这似乎是正确的,你能发布你的代码吗?你能解释一下你所说的$r是指每个组合中元素的最小数量吗?这个想法是用最少的$r
元素生成所有可能的组合。换句话说,我们需要包含$r
或更多元素的组合。希望这说明得很清楚?你能解释一下$r是指每个组合中元素的最小数量吗?这个想法是用最少的$r
元素生成所有可能的组合。换句话说