小编典典

所有可能的组合,如何制作图案?PHP的?

algorithm

我正在执行消除不需要的组合的代码,例如I(ABCDE)不想AAA BBB AB BA只希望ABC ABD ABE
....如此等等,希望它对任何情况都有效,例如我确实工作过的示例代码那样:他在3对3上进行一组组合(1-6)3
…但我希望他对4对4或10到10组合进行(1-15)的组合。理解。

$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


阅读 255

收藏
2020-07-28

共1个答案

小编典典

原始代码:我刚刚添加了$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
)
 */
?>
2020-07-28