dqzg62440 2018-05-08 19:01
浏览 23
已采纳

所有可能在数字之间加+, - 或者什么都没有得到总和等于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

  • 写回答

1条回答 默认 最新

  • douqianmin5367 2018-05-09 08:42
    关注

    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 . "
    ";
    }
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b