你的举例不对吧?
三层循环就可以了
$exp = array(123, 132, 213, 231, 312, 321);
for($i = 0; $i < 6; $i++)
{
for($j = 0; $j < 6; $j++)
{
for($k = 0; $k < 6; $k++)
{
echo 'a:'.$exp[$k].';b:'.$exp[$j].';c:'.$exp[$i].'\n';
}
}
}
三层循环就可以了
$exp = array(123, 132, 213, 231, 312, 321);
for($i = 0; $i < 6; $i++)
{
for($j = 0; $j < 6; $j++)
{
for($k = 0; $k < 6; $k++)
{
echo 'a:'.$exp[$k].';b:'.$exp[$j].';c:'.$exp[$i].'\n';
}
}
}
这是一个游戏的模型,原型是3个人出牌,每个人第一次可以出1点,2点或者3点中任意一个,总共出三轮牌,但每人已经出过的牌不能出。我需要得到出牌的所有组合,输出是按照每局来。得到所有可能出现的牌局。
你的输出是:
a:123 b:123 c:123
a:132 b:132 c:132
....
我不明白这代表了什么?
哎,老了啊,自己画流程图写递归居然写不出来了。55555
a:123 b:123 c:123
a:132 b:123 c:123
a:213 b:123 c:123
...
仍然没明白你的输出的意思?
function array_exchange(&$arr, $key1, $key2) {
$tmp = $arr[$key1];
$arr[$key1] = $arr[$key2];
$arr[$key2] = $tmp;
}
// 不知道PHP有没调换数据2个下标的值的函数,所以才自己写。$aNums = $bNums = $cNums = array(1, 2, 3);for ($n=2; $n >= 0; $n--) {
$aKey = rand(0, $n);
srand(microtime()+1.11);
$bKey = rand(0, $n);
srand(microtime()+2.22);
$cKey = rand(0, $n); echo $aNums[$aKey] . $bNums[$bKey] . $cNums[$cKey] . "<br />";
array_exchange($aNums, $aKey, $n);
array_exchange($bNums, $bKey, $n);
array_exchange($cNums, $cKey, $n);
}
?>
手册上的种子生成函数 ~
function make_seed()
{
list($usec, $sec) = explode(' ', microtime());
return (float) $sec + ((float) $usec * 100000);
}
function make_seed()
{
list($usec, $sec) = explode(' ', microtime());
return (float) $sec + ((float) $usec * 100000);
}function array_exchange(&$arr, $key1, $key2) {
$tmp = $arr[$key1];
$arr[$key1] = $arr[$key2];
$arr[$key2] = $tmp;
}
// 不知道PHP有没调换数据2个下标的值的函数,所以才自己写。$aNums = $bNums = $cNums = array(1, 2, 3);for ($n=2; $n >= 0; $n--) {
$aKey = rand(0, $n);
srand(make_seed());
$bKey = rand(0, $n);
srand(make_seed());
$cKey = rand(0, $n);echo $aNums[$aKey] . $bNums[$bKey] . $cNums[$cKey] . "<br />";
array_exchange($aNums, $aKey, $n);
array_exchange($bNums, $bKey, $n);
array_exchange($cNums, $cKey, $n);
}
?>缩进没了。。why>?!~
就是
第一次 第二次 第三次
a 1 2 3
b 1 2 3
c 1 2 3
<?php
function fullSort($a,$count)
{
$result=array();
if($count==2){
$result[]=$a;
$result[]=array($a[1],$a[0]);
return $result;
}elseif($count==1){
return $a;
}
for($i=0;$i<$count;$i++){
$a0 =array_shift($a);
$ir = fullSort($a,$count-1);
foreach($ir as $r){
array_unshift($r,$a0);
$result[] = $r;
}
array_push($a,$a0);
}
return $result;
}$temp = array(1,2,3);
$length = count($temp);
$order = fullSort($temp,$length);
$order_count = count($order);for ($i=0;$i<$order_count;$i++)
{
for($j=0;$j<$order_count;$j++)
{
for($k=0;$k<$order_count;$k++)
{
echo "第",++$count,"种:<br>";
for($l=0;$l<$length;$l++)
{
$computer = $order[$i][$l];
$me = $order[$j][$l];
$against = $order[$k][$l];
echo "我出$me,电脑出$computer,对方出$against<br>";
}
echo "<br>";
}
}
}
?>