1

我正在编写一个代码来消除不需要的组合,例如我(ABCDE)不想要 AAA BBB AB BA 只想要 ABC ABD ABE .... 等等,希望它在任何情况下都有效,我确实工作的示例代码那样:他在 3 上做了一组组合 (1-6) 3 ...但我希望他在 (1-15) 中使用 4 上 4 或 10 到 10 的组合 .... 更好地参见示例理解。

$lista = array(1,2,3,4,5,6);
$b=1;
for ($i=0; $i<=3; $i++) {
    for ($j=$b; $j<=4;$j++) {
    //  printf('valor do j = '.$j.'<br>');
        for ($k=$j+1; $k<count($lista); $k++) {
            printf($lista[$i].$lista[$j].$lista[$k].'<br>');
        }
    }
    $b++;
}

结果

123
124
125
126
134
135
136
145
146
156
234
235
236
245
246
256
345
346
356
456

4

3 回答 3

4
require_once 'Math/Combinatorics.php';
$combinatorics = new Math_Combinatorics;
$set = range(1,6);
$combosWithoutRepetition = $combinatorics->combinations($set, 3);
foreach ($combosWithoutRepetition as $combo) {
    echo join(',', $combo), "\n";
}

http://pear.php.net/package/Math_Combinatorics

您不需要安装 pear,只需下载该软件包并使用它即可。

于 2012-07-15T02:57:05.163 回答
1

原始代码:https ://stackoverflow.com/a/2617080/661872我只是添加了$len一部分。

<?php 
// function to generate and print all N! permutations of $str. (N = strlen($str)).
function permute($str,$i,$n,$len) {
    global $ret;
    if ($i == $n){
        if(in_array(substr($str,0,$len),$ret)==false){$ret[]=substr($str,0,$len);}
    }else {
        for ($j = $i; $j < $n; $j++) {
            swap($str,$i,$j);
            permute($str, $i+1, $n, $len);
            swap($str,$i,$j); // backtrack.
        }
    }
}

// function to swap the char at pos $i and $j of $str.
function swap(&$str,$i,$j) {
    $temp = $str[$i];
    $str[$i] = $str[$j];
    $str[$j] = $temp;
}
$ret = array();
$str = "123456";
permute($str,0,strlen($str), 3); // call the function.


print_r($ret);
/**
 * Array
(
    [0] => 123
    [1] => 124
    [2] => 125
    [3] => 126
    [4] => 132
    [5] => 134
    [6] => 135
    [7] => 136
    [8] => 143
    [9] => 142
    [10] => 145
    [11] => 146
    [12] => 153
    [13] => 154
    [14] => 152
    [15] => 156
    [16] => 163
    [17] => 164
    [18] => 165
    [19] => 162
    [20] => 213
    [21] => 214
    [22] => 215
    [23] => 216
    [24] => 231
    [25] => 234
    [26] => 235
    [27] => 236
    [28] => 243
    [29] => 241
    [30] => 245
    [31] => 246
    [32] => 253
    [33] => 254
    [34] => 251
    [35] => 256
    [36] => 263
    [37] => 264
    [38] => 265
    [39] => 261
    [40] => 321
    [41] => 324
    [42] => 325
    [43] => 326
    [44] => 312
    [45] => 314
    [46] => 315
    [47] => 316
    [48] => 341
    [49] => 342
    [50] => 345
    [51] => 346
    [52] => 351
    [53] => 354
    [54] => 352
    [55] => 356
    [56] => 361
    [57] => 364
    [58] => 365
    [59] => 362
    [60] => 423
    [61] => 421
    [62] => 425
    [63] => 426
    [64] => 432
    [65] => 431
    [66] => 435
    [67] => 436
    [68] => 413
    [69] => 412
    [70] => 415
    [71] => 416
    [72] => 453
    [73] => 451
    [74] => 452
    [75] => 456
    [76] => 463
    [77] => 461
    [78] => 465
    [79] => 462
    [80] => 523
    [81] => 524
    [82] => 521
    [83] => 526
    [84] => 532
    [85] => 534
    [86] => 531
    [87] => 536
    [88] => 543
    [89] => 542
    [90] => 541
    [91] => 546
    [92] => 513
    [93] => 514
    [94] => 512
    [95] => 516
    [96] => 563
    [97] => 564
    [98] => 561
    [99] => 562
    [100] => 623
    [101] => 624
    [102] => 625
    [103] => 621
    [104] => 632
    [105] => 634
    [106] => 635
    [107] => 631
    [108] => 643
    [109] => 642
    [110] => 645
    [111] => 641
    [112] => 653
    [113] => 654
    [114] => 652
    [115] => 651
    [116] => 613
    [117] => 614
    [118] => 615
    [119] => 612
)
 */
?>
于 2012-07-15T03:10:39.237 回答
1

我编写了一个类来处理处理二项式系数的常用函数,这是您的问题所属的问题类型。它执行以下任务:

  1. 将任何 N 选择 K 的所有 K 索引以良好的格式输出到文件。K-indexes 可以替换为更具描述性的字符串或字母。这种方法使得解决这类问题变得非常简单。

  2. 将 K 索引转换为已排序二项式系数表中条目的正确索引。这种技术比依赖迭代的旧已发布技术快得多。它通过使用帕斯卡三角形固有的数学属性来做到这一点。我的论文谈到了这一点。我相信我是第一个发现并发表这种技术的人,但我可能是错的。

  3. 将已排序二项式系数表中的索引转换为相应的 K 索引。我相信它可能比您找到的链接更快。

  4. 使用Mark Dominus方法计算二项式系数,它不太可能溢出并且适用于更大的数字。

  5. 该类是用 .NET C# 编写的,并提供了一种通过使用通用列表来管理与问题相关的对象(如果有)的方法。此类的构造函数采用一个名为 InitTable 的 bool 值,当它为 true 时,将创建一个通用列表来保存要管理的对象。如果此值为 false,则不会创建表。无需创建表即可执行上述 4 种方法。提供了访问器方法来访问表。

  6. 有一个关联的测试类,它显示了如何使用该类及其方法。它已经用 2 个案例进行了广泛的测试,并且没有已知的错误。

要了解此类并下载代码,请参阅制表二项式系数

将此类转换为 Perl 应该不难。另一种选择可能是将其转换为 Java,然后从 Perl 调用它。

从上面的示例中,您似乎正在使用 6 选择 3 的情况,这意味着一次有 6 个可能的项目要取 3 个。因此,执行此操作的一些示例代码如下所示:

// Create the binomial coefficient object for the 6 choose 3 case and
// do not bother to create the list of objects table.
BinCoeff<int> BC = new BinCoeff<int>(6, 3, false);
int[] KIndexes = new int[3];
int NumCombos6Choose3 = BinCoeff<int>.GetBinCoeff(6, 3);
// Loop through all of the combinations for this case.
for (int Loop = 0; Loop < NumCombos6Choose3; Loop++)
{
   // Get the K-Indexes for this combination.
   // The combinations are returned in rank order.
   BC.GetKIndexes(Loop, KIndexes);
   // Print out the K-Indexes or any other processing.
   ...
}
于 2012-09-26T15:02:28.693 回答