【问题标题】:Is there an algorithm to output all possible combinations of two sets of elements?是否有一种算法可以输出两组元素的所有可能组合?
【发布时间】:2012-10-04 23:55:16
【问题描述】:

我正在用 PHP 编写一个应用程序,用户可以在其中输入两组信息并打印出这两组信息的所有可能组合。目标是通过练习不同的问答模式,将其用作学习语言的教学工具。

例如,我们可以练习“您是否尝试过...?”这个问题。加上四项活动之一,如蹦极、帆伞运动、跳伞和水肺潜水。还有四种可能的答案:

  1. 是的,我有,我很享受。
  2. 是的,我有,但我不喜欢它。
  3. 不,我没有,但我想试试。
  4. 不,我没有,我不想尝试。

用户将输入两组数据,然后应用程序将打印包含所有可能的问题和答案组合的卡片。例如,卡片 1 如下所示:

  1. 跳伞:是的,我有,我很享受。
  2. 水肺潜水:是的,我有,但我不喜欢。
  3. 滑翔伞:不,我没有,但我想试试。
  4. 蹦极:不,我没有,我不想尝试。

下一张卡片可能如下所示:

  1. 跳伞:是的,我有,但我不喜欢。
  2. 水肺潜水:不,我没有,但我想试试。
  3. 滑翔伞:不,我没有,我不想尝试。
  4. 蹦极:是的,我有,而且我很喜欢。

如您所见,有许多不同的可能组合。这个想法是两个列表的长度相同,以允许打印出所有的可能性。同样重要的是,任何一张卡片上的问题或答案都不能重复使用一次,并且两张卡片可以相同。解决此问题的最佳方法是什么?实际的输入和输出不是问题——我以前做过类似的事情。我只需要生成组合的算法。

编辑:我想我真正想要的是让每张卡片的活动顺序相同​​,但有所有可能的答案组合。所以我真正需要的是能够生成以下一组索引,以便从 answers 数组中获取数据。所以我真的想要更像这样的东西:

  • 0,1,2,3
  • 0,1,3,2
  • 0,2,1,3
  • 0,2,3,1
  • 0,3,1,2
  • 0,3,2,1
  • 1,0,2,3
  • 1,0,3,2
  • 1,2,0,3
  • 1,2,3,0

...以此类推,直到产生所有可能的组合。

【问题讨论】:

  • 基本算法是遍历你的第一个元素,然后在那个循环中,遍历你的第二个元素
  • 这只会给出有限数量的组合,它们只是按顺序排列(例如:卡片 1:0-0、1-1、2-2、3-3;卡片 2:0- 1、1-2、2-3、3-0 等)。我还想要所有不按顺序排列的组合(例如:卡片 12:0-2、1-1、2-0、3-3;卡片 21:0-3、1-1、2-0、3- 2).
  • @blainarmstrong 由于已编辑的问题已经澄清了您想要的内容,因此我添加了一个新答案(即使它不再正确,我也会保留旧答案,因为我觉得它回答了这个问题我和其他人当时认为的标准)。

标签: php algorithm combinations


【解决方案1】:

试试这个:

$activities = array(); // Input all your activities as elements here.
$responses = array(); // Input all your responses as elements here.

foreach ($activities as $activity) {
    foreach ($responses as $response) {
        echo $activities.' '.$response."\n";
    }
}

【讨论】:

  • 请注意,这不会随机化“卡片”。只是逻辑输出。但这个“问题”可以通过向邻居的孩子提供这些卡片并说:“让我们玩一个游戏:它被称为将所有卡片扔到空中来解决。”
【解决方案2】:

好的,有了新的标准,我想我理解得更好了。

尝试递归。我的解决方案非常混乱,但我可以引导您完成它:

$activities = array('a', 'b', 'c', 'd'); // Input all your activities as elements here.
$responses = array(1, 2, 3, 4); // Input all your responses as elements here.

// Recursive function outputCombos accepts both arrays (for eventual output).
// $workingArray is the array of the current branch we're working with.
function outputCombos ($activities, $responses, $workingArray) {
    // Once the working array has been loaded to the maximum amt, print everything out.
    if (count($workingArray) == count($responses)) {
        echo "Combo\n";
        for ($x = 0; $x < count($activities); $x++) {
            echo $activities[$x].'::'.$workingArray[$x]."\n";
        }
    // If the working array isn't full, add an element that isn't currently in the working array, and recursively run the function again.
    } else {
        foreach ($responses as $response) {
            // Iterate through list of all possible responses, add it into a new working array and run the function if the response hasn't been used in this working array.
            if (!in_array($response, $workingArray)) {
                $newArray = $workingArray;
                $newArray[] = $response;
                outputCombos($activities, $responses, $newArray);
            }
        }
    }
}

foreach ($responses as $response) {
    echo '<pre>';
    // Start each branch of tree with unique response (should be 4 in this case).
    outputCombos($activities, $responses, array($response));
    echo '</pre>';
}

【讨论】:

  • 这看起来更像是我想要做的。有时间我会试一试。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-10
  • 2023-03-02
  • 2022-09-28
  • 1970-01-01
  • 2018-03-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多