【问题标题】:PHP: permutations formula but for distinct valuesPHP:排列公式,但用于不同的值
【发布时间】:2018-03-22 10:31:47
【问题描述】:

我一直试图从一个简单的公式中得到结果 并且无法完成正确的结果:

例如: 数组 A,B,C

可以返回6个结果: 回答: a, b | 一个,c | b, 一个 | b, c | c, 一个 | c, b

但是:

b, a 已被使用:a, b

c,b 已使用:b,c

c, a 已被使用:a, c

真的只留下 3 个不同的答案。

我一直使用的方法是:

function permutations($pool, $r = null) {
    $n = count($pool);

    if ($r == null) {
        $r = $n;
    }

    if ($r > $n) {
        return;
    }

    $indices = range(0, $n - 1);
    $cycles = range($n, $n - $r + 1, -1); // count down

    yield array_slice($pool, 0, $r);

    if ($n <= 0) {
        return;
    }

    while (true) {
        $exit_early = false;
        for ($i = $r;$i--;$i >= 0) {
            $cycles[$i]-= 1;
            if ($cycles[$i] == 0) {
                // Push whatever is at index $i to the end, move everything back
                if ($i < count($indices)) {
                    $removed = array_splice($indices, $i, 1);
                    array_push($indices, $removed[0]);
                }
                $cycles[$i] = $n - $i;
            } else {
                $j = $cycles[$i];
                // Swap indices $i & -$j.
                $i_val = $indices[$i];
                $neg_j_val = $indices[count($indices) - $j];
                $indices[$i] = $neg_j_val;
                $indices[count($indices) - $j] = $i_val;
                $result = [];
                $counter = 0;
                foreach ($indices as $indx) {
                    array_push($result, $pool[$indx]);
                    $counter++;
                    if ($counter == $r) break;
                }
                yield $result;
                $exit_early = true;
                break;
            }
        }
        if (!$exit_early) {
            break; // Outer while loop
        }
    }
}

//$result = iterator_to_array(permutations(['a', 'b', 'c', "d", "e", "f", "g", "h", "i", "j", "k" ,"l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"], 2));
$result = iterator_to_array(permutations(['a', 'b', 'c'], 2));
foreach ($result as $row) {
    print implode(", ", $row) . "<br>";
}
echo count($result);

【问题讨论】:

    标签: php math permutation


    【解决方案1】:

    我已经实现了自己的逻辑。希望对你有所帮助:

    <?php
    $string = 'a,b,c';
    $a = explode(',',$string);
    
    foreach($a as $k => $v){
            $count = count($a);
            for($i = 0; $i < $count; $i ++){
                    if($v != $a[$i]){
                            $allCases[] = $v.$a[$i];
                    }
            }
    }
    
    echo "Total cases found : ".implode(',',$allCases)."\n";
    
    foreach($allCases as $k1 => $v1){
        $allCases[$k1] = sortWord($v1);
    }
    
    $allCases = array_unique($allCases);
    
    echo "Distinct case : ".implode(',',$allCases)."\n";
    
    function sortWord($string){
            $stringParts = str_split($string);
            sort($stringParts);
            return implode('', $stringParts);
    }
    

    【讨论】:

      猜你喜欢
      • 2014-10-16
      • 2011-02-15
      • 2012-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-29
      • 1970-01-01
      相关资源
      最近更新 更多