用python算24及原理详解

一、描述

给出4个小于10的正整数,使用加、减、乘、除4种运算以及括号把4个数连接起来得到一个结果等于24的表达式。‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬

这里加、减、乘、除以及括号的运算结果和运算优先级跟平常定义一致。‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬

         例如,对于5,5,5,1,可知5×(5-1/5)=24。又如,对于1,1,4,2无论如何都不能得到24

输入格式

     在代码中的输入部分输入4个小于10的正整数。

输出格式

      对于每一组测试数据,输出一行,如果可以得到24,输出"YES"其算法;否则“NO”。

二、大致思路

将四个数字进行全排列,在他们之间添加运算符号,最后将数字和操作符进行拼接运算。

运算符我们需要进行排列组合,因为只有四个数字,所以只需要三个运算符,而且算法符可能会重复,比如三个都是+。

再遍历四个数字的全排列,对每一组数字而言,遍历所有组合的操作符。最后将数字和操作符进行拼接运算,就可以得到最终结果了。

三、具体代码


from itertools import permutations
 
a = int(input("请输入第1个数字:"))
b = int(input("请输入第2个数字:"))
c = int(input("请输入第3个数字:"))
d = int(input("请输入第4个数字:"))
list1 = [a, b, c, d]
p=[c for c in permutations(list1,4)] #对4个整数随机排列的列表
 
symbols = ["+", "-", "*", "/"]
 
list2 =[]     #算出24的排列组合的列表
 
flag=False
 
for n in p:
    one,two,three,four=n    #p中每个元素有4个整数组成
    for s1 in symbols:
        for s2 in symbols:
            for s3 in symbols:
                if s1+s2+s3=="+++" or s1+s2+s3=="***":
                    express = [ "{0}{1}{2}{3}{4}{5}{6}".format(one,s1, two,s2,three,s3,four)]  #全加或者乘时,括号已经没有意义。
                else:
                    express = [ "(({0}{1}{2}){3}{4}){5}{6}".format(one, s1, two, s2, three, s3, four),
                               "({0}{1}{2}){3}({4}{5}{6})".format(one, s1, two, s2, three, s3, four),
                               "(({0}{1}({2}{3}{4})){5}{6})".format(one, s1, two, s2, three, s3, four),
                               "{0}{1}(({2}{3}{4}){5}{6})".format(one, s1, two, s2, three, s3, four),
                               "{0}{1}({2}{3}({4}{5}{6}))".format(one, s1, two, s2, three, s3, four)]
                
                for e in express:
                    try:
                        if eval(e) == 24:
                            list2.append(e)
                            flag=True
                    except ZeroDivisionError:
                        pass

list3=set(list2)  #去除重复项
 
for c in list3:
    print("YES"+" "+c)
 
if flag==False:
    print("NO")

 

 

四、详细解析

1、首先我们对所有数字进行去全排列,这里我们使用 itertools.permutations 来帮助我们完成。

iertools.permutations 用法演示

from itertools import permutations
a = int(input("请输入第1个数字:"))
b = int(input("请输入第2个数字:"))
c = int(input("请输入第3个数字:"))
d = int(input("请输入第4个数字:"))
list1 = [a, b, c, d]
p=[c for c in permutations(list1,4)]

print(p)

结果显示

请输入第1个数字:2
请输入第2个数字:3
请输入第3个数字:4
请输入第4个数字:5
[(2, 3, 4, 5), (2, 3, 5, 4), (2, 4, 3, 5), (2, 4, 5, 3), 
(2, 5, 3, 4), (2, 5, 4, 3), (3, 2, 4, 5), (3, 2, 5, 4),
(3, 4, 2, 5), (3, 4, 5, 2), (3, 5, 2, 4), (3, 5, 4, 2),
(4, 2, 3, 5), (4, 2, 5, 3), (4, 3, 2, 5), (4, 3, 5, 2),
(4, 5, 2, 3), (4, 5, 3, 2), (5, 2, 3, 4), (5, 2, 4, 3),
(5, 3, 2, 4), (5, 3, 4, 2), (5, 4, 2, 3), (5, 4, 3, 2)]
2、然后我们需要拿到所有的操作运算符的所有组合方式。
3、Python List.append()用法
用于在列表末尾添加新的对象。
语法:List.append(obj) #
obj -- 添加到列表末尾的对象。
实例:
aList = [123, 'xyz', 'zara', 'abc']
aList.append( 2009 )
print ("Updated List : ", aList)

结果显示:

Updated List :  [123, 'xyz', 'zara', 'abc', 2009]

 

posted @ 2020-03-26 00:49  Mindf  阅读(1502)  评论(0)    收藏  举报