问题描述
我必须构建一个函数,该函数将列出在1到9之间的数字之间加+,-或不加任何内容并打印结果的所有可能性.
I have to built a function which would list the all possibilities to put +, - or nothing between the numbers ranging from 1 to 9 and print it result.
例如: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,依此类推.
For example: 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 and so on.
在注意每个表达式必须具有1到9之间的所有数字的同时,序列也应保持完整.下面是我的代码,可以达到目的,但是并不能生成所有可能的组合.
While taking care that every expression must have all the numbers present between 1 to 9 also there sequence should be intact. Below is my code to achieve the result but it does not generate all possible combination.
$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
它已经生成了一些组合,但并非所有可能的组合
It has generated some combinations but not all possible combinations
推荐答案
您可以通过递归操作-使用以下代码:
You can do it by recursive - Use the following code:
<?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";
}
?>
这篇关于所有可能在数字之间加上+,-或不加零以获得等于100的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!