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

我正在执行消除不需要的组合的代码,例如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++;
}

结果

123124125126134135136145146156234235236245246256345346356456

解决方法:

原始代码:https://*.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
)
 */
?>
上一篇:在python中合并列表


下一篇:javascript – 如何将x个玩家随机多次分成两队,每次都不同?