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

后端 未结 2 1771
再見小時候
再見小時候 2020-12-20 09:43

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条回答
  •  礼貌的吻别
    2020-12-20 10:38

    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)
    

提交回复
热议问题