我必须列出4个字符A-Z,a-z,0-9的所有可能的限制以及所有这些的组合。我如何通过所有可能的组合并打印它们?
它的用途是什么:我需要在一个html文档中创建它,然后我可以打印并将所有这些作为我们大学的随机唯一用户名,这样学生就可以根据一个在使用时无效的唯一ID提供反馈。我无法将此程序改为更好的程序!
答案 0 :(得分:6)
警告:这需要一些时间来计算,因为有62 ^ 4 = 14776336种可能的组合。如果累积结果并且不直接打印它也会占用大量内存。
function print_combinations($characters, $length, $combination = '') {
if ($length > 0) {
foreach ($characters as $i) {
print_combinations($characters, $length - 1, $combination . $i);
}
} else {
printf("%s\n", $combination);
}
}
$characters = array_merge(range('A', 'Z'), range('a', 'z'), range(0, 9));
print_combinations($characters, 4);
答案 1 :(得分:2)
使用非常规方法,您可以使用comments on the php documentation for base_convert中的dec2any,如下所示:
$index = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$base = strlen($index);
$len = 4;
for ($i = 0, $l = pow(strlen($index), $len); $i < $l; $i++) {
echo str_pad(dec2any($i, $base, $index), $len, "0", STR_PAD_LEFT), "\n";
}
function dec2any( $num, $base=62, $index=false ) {
if (! $base ) {
$base = strlen( $index );
} else if (! $index ) {
$index = substr( "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" ,0 ,$base );
}
$out = "";
for ( $t = floor( log10( $num ) / log10( $base ) ); $t >= 0; $t-- ) {
$a = floor( $num / pow( $base, $t ) );
$out = $out . substr( $index, $a, 1 );
$num = $num - ( $a * pow( $base, $t ) );
}
return $out;
}
通过更改$ index和$ len可以很容易地进行调整。
答案 2 :(得分:0)
有点匆忙,但是:
class Combinations{
protected $characters = array();
protected $combinations = array();
public function __construct($characters){
$this->characters = $characters;
}
public function getCombinations($length){
foreach($this->characters as $comb)
{
$this->getRecurse($comb, $length - 1);
}
$combinations = $this->combinations;
$this->combinations = array();
return $combinations;
}
public function getRecurse($combination, $length){
if($length <= 0){
$this->combinations[] = $combination;
}else{
foreach($this->characters as $comb)
{
$this->getRecurse($combination.$comb, $length - 1);
}
}
}
}
$characters = array_merge(range('A', 'Z'), range('a', 'z'), range(0, 9));
$comb = new Combinations($characters);
print_r( $comb->getCombinations(4) );
传递给getCombinations
的数字是您希望组合的时长。
答案 3 :(得分:0)
更具普遍性的分隔符方法:
function getCombinations($base,$delimiter="|"){
$base = array_values($base);
$baselen = count($base);
if($baselen == 0){
return;
}
if($baselen == 1){
return $base[0];
}else{
//get one level lower combinations
$oneLevelLowerArray = $base;
$orig_part = $oneLevelLowerArray[0];
unset($oneLevelLowerArray[0]);
$oneLevelLower = getCombinations($oneLevelLowerArray,$delimiter);
$countOLL = count($oneLevelLower);
foreach ($orig_part as $rowa) {
foreach ($oneLevelLower as $rowb) {
$resultArray[] = $rowa.$delimiter.$rowb;
}
}
}
return $resultArray;
}
答案 4 :(得分:-1)
那样的东西? (伪代码)
$a = array_merge(range('A', 'Z'), range('a', 'z'), range(0, 9));
foreach($a as $key => $b) {
echo $b.$a[$key+1].$a[$key+2].$a[$key+3].'<br />';
}
答案 5 :(得分:-1)
$numbers = range(0, 9);
$lowerCaseLetters = range('a', 'z');
$upperCaseLetters = range('A', 'Z');
foreach($numbers as $number) {
foreach($lowerCaseLetters as $lowerCaseLetter) {
foreach($uperCaseLetters as $upperCaseLetter) {
echo $number.$lowerCaseLetter.$upperCaseLetter.'<br />';
echo $number.$upperCaseLetter.$lowerCaseLetter.'<br />';
echo $lowerCaseLetter.$number.$upperCaseLetter.'<br />';
echo $lowerCaseLetter.$upperCaseLetter.$number.'<br />';
echo $upperCaseLetter.$lowerCaseLetter.$number.'<br />';
echo $upperCaseLetter.$number.$lowerCaseLetter.'<br />';
}
}
}