所有可能在数字之间加+, - 或者什么都没有得到总和等于100

时间:2018-05-08 19:01:00

标签: php loops combinations

我必须构建一个函数,列出所有可能放置+, - 或者在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

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

2 个答案:

答案 0 :(得分:4)

您可以通过递归执行此操作 - 使用以下代码:

<?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";
}

?>

答案 1 :(得分:0)

def hun(exp, i):
        if (i<=9):
                for mark in ("+", "-", ""):
                        hun(exp + mark + str(i), i+1)
        elif eval(exp) == 100:
                print(exp + " = 100")
hun("1",2)