用python算24点及原理详解

1 描述

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

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

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

1.1 输入格式

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

1.2 输出格式

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

2 大致思路

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

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

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

3 知识点补充

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

iertools.permutations 用法演示

import itertools

a = int(input("请输入第1个数字:"))
b = int(input("请输入第2个数字:"))
c = int(input("请输入第3个数字:"))
d = int(input("请输入第4个数字:"))

my_list = [a, b, c, d]
result = [c for c in itertools.permutations(my_list, 4)]

for i, r in enumerate(result):
    if i % 4 == 0:
        print()
    print(r, end="\t")
print("\n\n长度为:", len(result))

运行结果:

请输入第1个数字:1
请输入第2个数字:2
请输入第3个数字:3
请输入第4个数字:4

(1, 2, 3, 4)	(1, 2, 4, 3)	(1, 3, 2, 4)	(1, 3, 4, 2)	
(1, 4, 2, 3)	(1, 4, 3, 2)	(2, 1, 3, 4)	(2, 1, 4, 3)	
(2, 3, 1, 4)	(2, 3, 4, 1)	(2, 4, 1, 3)	(2, 4, 3, 1)	
(3, 1, 2, 4)	(3, 1, 4, 2)	(3, 2, 1, 4)	(3, 2, 4, 1)	
(3, 4, 1, 2)	(3, 4, 2, 1)	(4, 1, 2, 3)	(4, 1, 3, 2)	
(4, 2, 1, 3)	(4, 2, 3, 1)	(4, 3, 1, 2)	(4, 3, 2, 1)	

长度为: 24

4 具体代码

from itertools import permutations

a = int(input("请输入第1个数字:"))
b = int(input("请输入第2个数字:"))
c = int(input("请输入第3个数字:"))
d = int(input("请输入第4个数字:"))
my_list = [a, b, c, d]
# 对4个整数随机排列的列表
result = [c for c in permutations(my_list, 4)]

symbols = ["+", "-", "*", "/"]

list2 = []  # 算出24的排列组合的列表

flag = False

for one, two, three, four in result:
    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 not flag:
    print("NO!")

 5 BUG修复

感谢qq_44273902博主提出程序BUG,现进行BUG修复

5.1 问题分析

5.1.1 问题复现

输入3、3、8、8,进行问题复现

输出结果如下: 

针对输出结果进行分析,明明是有正确答案的,为何还输出NO!???

5.1.2 原因分析

运行以下代码流程

print(8/(3-(8/3)))

运行结果:

 

了然,精度问题

5.2 针对问题进行解决

既然是由于精度问题造成的,那么可以约定保留6位小数点(当然也可以保留其他位数的小数),如此一来即可解决该问题。

将第36行代码

if eval(e) == 24:

改成

if round(eval(e), 6) == 24:

优化之后的代码如下:

from itertools import permutations

a = int(input("请输入第1个数字:"))
b = int(input("请输入第2个数字:"))
c = int(input("请输入第3个数字:"))
d = int(input("请输入第4个数字:"))
my_list = [a, b, c, d]
# 对4个整数随机排列的列表
result = [c for c in permutations(my_list, 4)]

symbols = ["+", "-", "*", "/"]

list2 = []  # 算出24的排列组合的列表

flag = False
print(result)

for one, two, three, four in result:
    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)]
                # print(one + two + three + four)
                    if str(one) + str(two) + str(three) + str(four) == "8383":
                        print(express)

                for e in express:
                    try:
                        # if eval(e) == 24:
                        if round(eval(e), 6) == 24:
                            list2.append(e)
                            flag = True
                    except ZeroDivisionError:
                        pass

list3 = set(list2)  # 去除重复项

for c in list3:
    print("YES:", c)

if not flag:
    print("NO!")

@property是Python中的一个内置修饰符,用于将一个方法变成属性调用。在面向对象编程中,有时候我们希望通过调用对象的属性来获得或设置其值,而不是直接调用方法。这样可以使代码更加简洁和易读。 @property的使用方式是,在需要被修饰的方法上方加上@property装饰符。这样,当我们调用对象的该方法时,就会像访问属性一样来访问。 @property修饰的方法通常用于获取属性的值,因此被称为getter方法。此外,我们还可以使用@property来定义setter方法,用于设置属性的值。 @property与getter和setter方法的原理是通过实现特殊的方法来实现的。 对于getter方法,@property将其装饰的方法转换为只读的属性。当我们通过调用该属性时,实际上调用的是被装饰的方法。 对于setter方法,@方法名.setter装饰器用来定义一个名字与被装饰方法相同的方法,并通过该方法来修改属性的值。当我们给该属性赋值时,实际上调用的是被装饰的setter方法。 通过getter和setter方法的配合,@property可以使我们在获取和设置属性值时更加灵活和方便。比如,我们可以在setter方法中进行属性值的校验和处理,确保属性值符合预期。 总结起来,@property提供了一种优雅和方便的方式来定义类的属性访问方法,使得我们可以像访问属性一样来调用这些方法。它的实现原理是通过装饰器和特殊的方法来实现的。这种方式使得代码更加简洁、易读,并且拥有更高的灵活性。
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值