所有可能的组合,如何让一个模式? PHP? [英] All possible combinations, how to make a pattern? PHP?
问题描述
我做了code,消除不需要的组合,如我(ABCDE)不想AAA BBB AB BA只想ABC,ABD ABE ....等等,希望它是适用于任何情况下,例如code,我没工作方式:他让一组的组合(1-6)3月3 ...但我想他(1-15)funciane有4对组合,4个或10至10 ....参见更好的理解的例子。
I'm doing a code that eliminates unneeded combinations such as I (ABCDE) do not want AAA BBB AB BA only want ABC ABD ABE .... so on, want it to be valid for any situation, example code that I did work that way: he makes a set of combinations (1-6) 3 on 3 ... but I want him funciane of (1-15) with combinations of 4 on 4 or 10 to 10 .... See the example for better understanding.
$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
123
124
125
126
134
135
136
145
146
156
234
235
236
245
246
256
345
346
356
456
推荐答案
原来的code: http://stackoverflow.com /一/六十六万一千八百七十二分之二百六十一万七千〇八十我只是添加 $ LEN
部分。
Original code: http://stackoverflow.com/a/2617080/661872 I just added $len
part.
<?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
)
*/
?>
这篇关于所有可能的组合,如何让一个模式? PHP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!