php – 所有可能的放置, – 或数字之间没有任何东西得到总和等于100

我必须构建一个函数,列出所有可能的放置, – 或者在1到9之间的数字之间没有任何内容,并打印出结果.

例如:
1 2 3 4 5 6 7 8 9 = 45,
1 2 3 4 5 6 7 8-9 = 25,
1 2 3 4 5 6 7 89 = 117,
1 2 3 45-67 89 = 73等等.

虽然注意每个表达式必须包含1到9之间的所有数字,但序列应该是完整的.下面是我的代码,以实现结果,但它不会产生所有可能的组合.

$characters = ['+', '-', ''];
$opr[0] = $characters[0];
$opr[1] = $characters[0];
$opr[2] = $characters[0];
$opr[3] = $characters[0];
$opr[4] = $characters[0];
$opr[5] = $characters[0];
$opr[6] = $characters[0];
$opr[7] = $characters[0];
for($j=0;$j<=7;$j++) {
    for($l=7;$l>=0;$l--) {
        for($i=0;$i<=2;$i++) {
            if( $l !== $j ) {
                $opr[$l] = $characters[$i];
            }
            $string = "1".$opr[0]."2".$opr[1]."3".$opr[2]."4".$opr[3]."5".$opr[4]."6".$opr[5]."7".$opr[6]."8".$opr[7]."9";
            $sum = eval('return '.$string.';');
            echo $string."="."$sum<br />";
        }
    }

}

这是我的代码的输出:

1+2+3+4+5+6+7+8+9=45
1+2+3+4+5+6+7+8-9=27
1+2+3+4+5+6+7+89=117
1+2+3+4+5+6+7+89=117
1+2+3+4+5+6+7-89=-61
1+2+3+4+5+6+789=810
1+2+3+4+5+6+789=810
1+2+3+4+5+6-789=-768
1+2+3+4+5+6789=6804
1+2+3+4+5+6789=6804
1+2+3+4+5-6789=-6774
1+2+3+4+56789=56799
1+2+3+4+56789=56799
1+2+3+4-56789=-56779
1+2+3+456789=456795
1+2+3+456789=456795
1+2+3-456789=-456783
1+2+3456789=3456792
1+2+3456789=3456792
1+2-3456789=-3456786
1+23456789=23456790
1+23456789=23456790
1+23456789=23456790
1+23456789=23456790
1+2345678+9=2345688
1+2345678-9=2345670
1+23456789=23456790
1+234567+89=234657
1+234567-89=234479
1+23456789=23456790
1+23456+789=24246
1+23456-789=22668
1+23456789=23456790
1+2345+6789=9135
1+2345-6789=-4443
1+23456789=23456790
1+234+56789=57024
1+234-56789=-56554
1+23456789=23456790
1+23+456789=456813
1+23-456789=-456765
1+23456789=23456790
1+23456789=23456790
1+23456789=23456790
1+23456789=23456790
1+23456789=23456790
1-23456789=-23456788
123456789=123456789
12345678+9=12345687
12345678-9=12345669
123456789=123456789
1234567+89=1234656
1234567-89=1234478
123456789=123456789
123456+789=124245
123456-789=122667
123456789=123456789
12345+6789=19134
12345-6789=5556
123456789=123456789
1234+56789=58023
1234-56789=-55555
123456789=123456789
123456789=123456789
123456789=123456789
123456789=123456789
12+3456789=3456801
12-3456789=-3456777
123456789=123456789
1+23456789=23456790
1-23456789=-23456788
123456789=123456789
12345678+9=12345687
12345678-9=12345669
123456789=123456789
1234567+89=1234656
1234567-89=1234478
123456789=123456789
123456+789=124245
123456-789=122667
123456789=123456789
12345+6789=19134
12345-6789=5556
123456789=123456789
123456789=123456789
123456789=123456789
123456789=123456789
123+456789=456912
123-456789=-456666
123456789=123456789
12+3456789=3456801
12-3456789=-3456777
123456789=123456789
1+23456789=23456790
1-23456789=-23456788
123456789=123456789
12345678+9=12345687
12345678-9=12345669
123456789=123456789
1234567+89=1234656
1234567-89=1234478
123456789=123456789
123456+789=124245
123456-789=122667
123456789=123456789
123456789=123456789
123456789=123456789
123456789=123456789
1234+56789=58023
1234-56789=-55555
123456789=123456789
123+456789=456912
123-456789=-456666
123456789=123456789
12+3456789=3456801
12-3456789=-3456777
123456789=123456789
1+23456789=23456790
1-23456789=-23456788
123456789=123456789
12345678+9=12345687
12345678-9=12345669
123456789=123456789
1234567+89=1234656
1234567-89=1234478
123456789=123456789
123456789=123456789
123456789=123456789
123456789=123456789
12345+6789=19134
12345-6789=5556
123456789=123456789
1234+56789=58023
1234-56789=-55555
123456789=123456789
123+456789=456912
123-456789=-456666
123456789=123456789
12+3456789=3456801
12-3456789=-3456777
123456789=123456789
1+23456789=23456790
1-23456789=-23456788
123456789=123456789
12345678+9=12345687
12345678-9=12345669
123456789=123456789
123456789=123456789
123456789=123456789
123456789=123456789
123456+789=124245
123456-789=122667
123456789=123456789
12345+6789=19134
12345-6789=5556
123456789=123456789
1234+56789=58023
1234-56789=-55555
123456789=123456789
123+456789=456912
123-456789=-456666
123456789=123456789
12+3456789=3456801
12-3456789=-3456777
123456789=123456789
1+23456789=23456790
1-23456789=-23456788
123456789=123456789
123456789=123456789
123456789=123456789
123456789=123456789
1234567+89=1234656
1234567-89=1234478
123456789=123456789
123456+789=124245
123456-789=122667
123456789=123456789
12345+6789=19134
12345-6789=5556
123456789=123456789
1234+56789=58023
1234-56789=-55555
123456789=123456789
123+456789=456912
123-456789=-456666
123456789=123456789
12+3456789=3456801
12-3456789=-3456777
123456789=123456789
1+23456789=23456790
1-23456789=-23456788
123456789=123456789

它产生了一些组合,但并非所有可能的组合

解决方法:

您可以通过递归来执行 – 使用以下代码:

<?php

function addNum($baseStr, $nextNum)
{
 if ($nextNum == 10)
     return array($baseStr);
 $arr1 = addNum($baseStr. '+' . $nextNum, $nextNum+1);
 $arr2 = addNum($baseStr. '-' . $nextNum, $nextNum+1);
 $arr3 = addNum($baseStr. '' . $nextNum, $nextNum+1);
    return array_merge($arr1, $arr2, $arr3);
}

$permutation = addNum('1', 2);
foreach ($permutation as $perm)
{
 $res = eval("return $perm;");
 echo $perm . '=' . $res . "\n";
}

?>
上一篇:javascript – 查找在一定范围内加起来为X的N个非重复数字的所有可能组合


下一篇:Java:给出数字N的true和false组合的生成器;