当前位置:实例文章 » PHP实例» [文章]PHP 排列组合

PHP 排列组合

发布人:shili8 发布时间:2022-12-03 14:11 阅读次数:23

初学PHP,闲暇写的排列组合,以十一运金为例,11,5的排列组合,可通用。(备注:初学,用字符串简单控制)。

<?php
$total = 0;
$result = array();
function test() {
combination(11,5,"",$total);


printf("Total %d", $GLOBALS['total']);

echo '<br />';

foreach ($GLOBALS['result'] as $value) {
echo implode('*',$value) . '<br />';
}
}
function combination($number,$count,$surfix) {
for($i=1;$i<=$number;$i++) {
$num = sprintf("%02d", $i);

if($count==1) {
if(strpos($surfix,$num)==false){
$result = $surfix. "*". $num;

$result = str_replace('A','',$result);
$temp = explode('*',$result);
sort($temp);
$key = implode('',$temp);

if($GLOBALS['result'][$key]==null) {
$GLOBALS['result'][$key]=$temp;
$GLOBALS['total']++;
}
unset($temp);
}
} else {
if($surfix=="") {
combination($number,$count-1,"A".$num);
} else {
if(strpos($surfix,$num)==false){
combination($number,$count-1,$surfix."*" .$num);
}
}
}
}
}
function permutation($number,$count,$surfix) {
for($i=1;$i<=$number;$i++) {
$num = sprintf("%02d", $i);

if($count==1) {
if(strpos($surfix,$num)==false){
$GLOBALS['total']++;
array_push($GLOBALS['result'], $surfix. "*". $num);
}
} else {
if($surfix=="") {
permutation($number,$count-1,"A".$num);
} else {
if(strpos($surfix,$num)==false){
permutation($number,$count-1,$surfix."*" .$num);
}
}
}
}
}
?>

相关标签:

免责声明

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱290110527@qq.com删除。

其他信息

其他资源

Top