Heim > Artikel > Backend-Entwicklung > ABCCBA从1到9不同数字组成算法
$a = Combination(range(0, 9), 3);foreach($a as $v) $r[] = join('', array_merge($v, array_reverse($v)));print_r($r);
Array( [0] => 789987 [1] => 689986 [2] => 679976 [3] => 678876 [4] => 589985 [5] => 579975 [6] => 578875 [7] => 569965 [8] => 568865 [9] => 567765 [10] => 489984 [11] => 479974 [12] => 478874 [13] => 469964 [14] => 468864 [15] => 467764 [16] => 459954 [17] => 458854 [18] => 457754 [19] => 456654 [20] => 389983 [21] => 379973 [22] => 378873 [23] => 369963 [24] => 368863 [25] => 367763 [26] => 359953 [27] => 358853 [28] => 357753 [29] => 356653 [30] => 349943 [31] => 348843 [32] => 347743 [33] => 346643 [34] => 345543 [35] => 289982 [36] => 279972 [37] => 278872 [38] => 269962 [39] => 268862 [40] => 267762 [41] => 259952 [42] => 258852 [43] => 257752 [44] => 256652 [45] => 249942 [46] => 248842 [47] => 247742 [48] => 246642 [49] => 245542 [50] => 239932 [51] => 238832 [52] => 237732 [53] => 236632 [54] => 235532 [55] => 234432 [56] => 189981 [57] => 179971 [58] => 178871 [59] => 169961 [60] => 168861 [61] => 167761 [62] => 159951 [63] => 158851 [64] => 157751 [65] => 156651 [66] => 149941 [67] => 148841 [68] => 147741 [69] => 146641 [70] => 145541 [71] => 139931 [72] => 138831 [73] => 137731 [74] => 136631 [75] => 135531 [76] => 134431 [77] => 129921 [78] => 128821 [79] => 127721 [80] => 126621 [81] => 125521 [82] => 124421 [83] => 123321 [84] => 089980 [85] => 079970 [86] => 078870 [87] => 069960 [88] => 068860 [89] => 067760 [90] => 059950 [91] => 058850 [92] => 057750 [93] => 056650 [94] => 049940 [95] => 048840 [96] => 047740 [97] => 046640 [98] => 045540 [99] => 039930 [100] => 038830 [101] => 037730 [102] => 036630 [103] => 035530 [104] => 034430 [105] => 029920 [106] => 028820 [107] => 027720 [108] => 026620 [109] => 025520 [110] => 024420 [111] => 023320 [112] => 019910 [113] => 018810 [114] => 017710 [115] => 016610 [116] => 015510 [117] => 014410 [118] => 013310 [119] => 012210)
function Combination( $arr, $num=0) { $arr = array_values($arr); $len = count($arr); if($num == 0) $num = $len; $res = array(); for($i=1,$n=pow(2, $len); $i<$n; ++$i) { $tmp = str_pad(base_convert($i, 10, 2), $len, '0', STR_PAD_LEFT); $t = array(); for($j=0; $j<$len; ++$j) { if($tmp{$j} == '1') { $t[] = $arr[$j]; } } if(count($t) == $num) $res[] = $t; } return $res; }
如果 012210 和 021120 不算重复的话,可以这样写
for($a=0; $a<10; $a++) for($b=0; $b<10; $b++) for($c=0; $c<10; $c++) if($a != $b && $a != $c && $b != $c) printf("%d%d%d%d%d%d\n", $a, $b, $c, $c, $b, $a);
012210013310014410015510016610017710018810019910021120023320024420025520026620027720......
谢谢!辛苦版主了.会编程真好.要是人工自己一个一个来.估计一天也搞不完.