Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
630 views
in Technique[技术] by (71.8m points)

php - All possibilities to put +, - or nothing between numbers to get sum equal to 100

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.

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.

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

}

Here is the output of my code:

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

?>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...