Php 如果一个产品有多个因素对,如何按产品对因素进行分组并显示?

Php 如果一个产品有多个因素对,如何按产品对因素进行分组并显示?,php,grouping,elements,multiplication,factors,Php,Grouping,Elements,Multiplication,Factors,使用预定义的数字数组,如何使用PHP生成一个多维数组,将所有因子对按其乘积分组 输入阵列: $array = array(1,2,3,4,5,6,7,8); 我想显示具有多个因子对的每个产品组的所有因子对 如果没有具有多个因子对的产品组,则应显示未找到因子对 鉴于上述输入,这是我的预期结果: 1 6 and 2 3 // product group = 6 1 8 and 2 4 // product group = 8 2 6 and 3 4 // product group =

使用预定义的数字数组,如何使用PHP生成一个多维数组,将所有因子对按其乘积分组

输入阵列:

$array = array(1,2,3,4,5,6,7,8);
  • 我想显示具有多个因子对的每个产品组的所有因子对

  • 如果没有具有多个因子对的产品组,则应显示
    未找到因子对

鉴于上述输入,这是我的预期结果:

1 6 and 2 3  // product group = 6
1 8 and 2 4  // product group = 8
2 6 and 3 4  // product group = 12
3 8 and 4 6  // product group = 24
*注意:随着输入数组大小的增加,输出将显示每组两个以上的因子对

这是我的C++代码:

 void findPairs(int arr[], int n)
{
    bool found = false;
    unordered_map<int, pair < int, int > > H;
    for (int i=0; i<n; i++)
    {
        for (int j=i+1; j<n; j++)
        {
            // If product of pair is not in hash table,
            // then store it
            int prod = arr[i]*arr[j];
            if (H.find(prod) == H.end())
                H[prod] = make_pair(i,j);

            // If product of pair is also available in
            // then print current and previous pair
            else
            {
                pair<int,int> pp = H[prod];
                cout << arr[pp.first] << " " << arr[pp.second]
                     << " and " << arr[i]<<" "<<arr[j]<<endl;
                found = true;
            }
        }
    }
    // If no pair find then print not found
    if (found == false)
        cout << "No pairs Found" << endl;
}
void findPairs(int arr[],int n)
{
bool-found=false;
无序的_-map>H;

对于(int i=0;i最简单的解决方案适用于像您的示例那样的小数组,但会为较大的输入使用大量内存。基本上,首先使用嵌套循环计算所有产品。对于每个产品,创建一个生成产品的输入列表。请注意,可能有两种以上的方法可以获得特定结果,因此您可以对于较大的列表,您可以获得类似于
11226和34code>的输出

对于大小为N的输入,需要在内存中存储((N-1)*N)/2个元组,所以这是需要记住的


最简单的解决方案适用于像您的示例那样的小数组,但会为较大的输入使用大量内存。基本上,首先使用嵌套循环计算所有产品。对于每个产品,创建生成产品的输入列表。请注意,可能有两种以上的方法来获得特定结果,因此您可能会得到一个对于较大的列表,输出如
1 12和2 6以及3 4

对于大小为N的输入,需要在内存中存储((N-1)*N)/2个元组,所以这是需要记住的

<代码> < P>这是你的C++代码“翻译”到PHP(主要是搜索和替换)。 90%的转换是通过删除变量类型并在变量名前加上
$
实现的。PHP类型是数组、列表和映射(又称哈希、字典)的混合体,可用于
$H
及其包含的值(值对)

这是你的C++代码“翻译”到PHP(主要是搜索和替换)。 90%的转换是通过删除变量类型并在变量名前加上

$
实现的。PHP类型是数组、列表和映射(又称哈希、字典)的混合体,可用于
$H
及其包含的值(值对)


我没有对我的方法进行速度测试,但我认为它更直接,更容易阅读。
基本上,它生成完整的多维数组,然后过滤掉只有一对的任何子数组,然后如果还有子数组,它会显示它们。简单

我的方法执行时没有任何
count()
调用,也没有增加关键变量。它使用非常快速的
isset()
调用过滤结果数组,并
array\u walk()
迭代和
内爆()
符合条件的子数组

作为一项额外功能,我使用
range()
动态生成输入数组,该数组通过输入数组的最高值来确定。当然,如果您想找到
[3,4,5]的对
,然后您必须修改此过程,或者简单地恢复到原始样式——这取决于您的项目期望

代码:()


我没有对我的方法进行速度测试,但我认为它更直接,更容易阅读。
基本上,它生成完整的多维数组,然后过滤掉只有一对的任何子数组,然后如果还有子数组,它会显示它们。简单

我的方法执行时没有任何
count()
调用,也没有增加关键变量。它使用非常快速的
isset()
调用过滤结果数组,并
array\u walk()
迭代和
内爆()
符合条件的子数组

作为一项额外功能,我使用
range()
动态生成输入数组,该数组通过输入数组的最高值来确定。当然,如果您想找到
[3,4,5]的对
,然后您必须修改此过程,或者简单地恢复到原始样式——这取决于您的项目期望

代码:()


在C++中,我可以做同样的事情,但在PHPI中,我同意,但是输出背后的逻辑意味着你是如何得到输出的,或者,你可以把你的C++代码Helikes 1×6=2×3=6作为Ab= CDST的一些代码。甚至C++代码也能帮助。逻辑是什么?我可以在C++中做同样的事情,但不是PHPI同意的,B。在输出背后的逻辑是指如何获得输出?或者,您可以将C++代码HeLeCK 1×6=2×3=6作为Ab= CDST后的某个代码。即使C++代码也可以帮助。为什么使用$NOF对;条件类似的AB=CDI增加了这个<代码> $NOFOPENS <代码>以防止不必要的循环。如果您想创建4个CONBIN,请假设。只需将其设置为
$noOfPairs=4;
为什么使用$noOfPairs;像pair ab=cdI这样的条件已添加了此
$noOfPairs
以防止不必要的循环。假设您要创建4个组合,只需将其设置为
$noOfPairs=4;
感谢@axiac提供了最佳答案感谢@axiac提供了最佳答案
$input = [1, 2, 3, 4, 5, 6, 7, 8];

$products = [];

foreach ($input as $index1 => $value1) {
    // Assuming you only want unique combinations, only combine this value with the other values coming after it
    for ($index2 = $index1 + 1; $index2 < count($input); $index2++) {
        $value2 = $input[$index2];
        $product = $value1 * $value2;

        // Make sure there is an entry in the $products array for adding this input to
        if (!isset($products[$product])) {
            $products[$product] = [];
        }

        // Add this input (formatted) to the list of possible inputs resulting in this product
        $products[$product][] = sprintf('%d %d', $value1, $value2);
    }
}

// Print all inputs resulting in the same products, if there are more than 1 way to produce the same output
foreach ($products as $inputs) {
    if (count($inputs) > 1) {
        echo implode(' and ', $inputs), PHP_EOL;
    }
}
1 6 and 2 3
1 8 and 2 4
2 6 and 3 4
3 8 and 4 6
<?php
ini_set("display_errors", 1);
$result=array();
$array = array(1,2,3,4,5,6,7,8);
$counter=0;
$noOfPairs=3;
while (count($result)!=$noOfPairs)
{
    shuffle($array);
    getPair($array);
}
print_r($result);
function getPair($array)
{
    global $result;
    $product=$array[0]*$array[1];
    if(isset($result[$product]))
    {
        return false;
    }
    $result[$product][]=array($array[0],$array[1]);
    unset($array[0]);
    unset($array[1]);
    foreach($array as $key1 => $value1)
    {
        foreach($array as $key2 => $value2)
        {
            if($value1*$value2==$product)
            {
                $result[$product][]=array($value1,$value2);
                break;
            }
        }
         if(count($result[$product])==2)
        {
            break;
        }
    }
    if(count($result[$product])==1)
    {
        unset($result[$product]);
    }
}
function findPairs(array $arr, $n)
{
    $found = false;
    $H = array();
    for ($i=0; $i<$n; $i++)
    {
        for ($j=$i+1; $j<$n; $j++)
        {
            // If product of pair is not in hash table,
            // then store it
            $prod = $arr[$i]*$arr[$j];
            if (! array_key_exists($prod, $H))
                $H[$prod] = array($i,$j);

            // If product of pair is also available in
            // then print current and previous pair
            else
            {
                $pp = $H[$prod];
                echo $arr[$pp[0]], " ", $arr[$pp[1]]
                     , " and ", $arr[$i], " ", $arr[$j], "\n";
                $found = true;
            }
        }
    }
    // If no pair find then print not found
    if ($found == false)
        echo "No pairs Found\n";
}

$array = array(1,2,3,4,5,6,7,8);
findPairs($array, count($array));
1 6 and 2 3
1 8 and 2 4
2 6 and 3 4
3 8 and 4 6
function findPairs($maxfactor){
    $array=range(1,$maxfactor); // spare yourself having to write out the array
    foreach($array as $v1){
        $array=array_slice($array,1); // shrink array as you go to avoid needless iterations
        foreach($array as $v2){
            $result[$v1*$v2][]="$v1 $v2";  // generate multi-dim output array using products as outer keys
        }
    }
    $result=array_filter($result,function($a){return isset($a[1]);});  // remove elements with < 2 pairs
    if(!$result){
        echo "No pairs found";
    }else{
        array_walk($result,function($a){echo implode(' and ',$a),"\n";});
    }
}
findPairs(8);
1 6 and 2 3
1 8 and 2 4
2 6 and 3 4
3 8 and 4 6