熟练写码的版友来解决一个算24问题

STEM版,合并数学,物理,化学,科学,工程,机械。不包括生物、医学相关,和计算机相关内容。

版主: verdeliteTheMatrix

回复
FGH楼主
论坛精英
论坛精英
帖子互动: 106
帖子: 7032
注册时间: 2022年 7月 25日 16:30

熟练写码的版友来解决一个算24问题

帖子 FGH楼主 »

请列出四个不超过10的自然数算24时中间产物必须出现分数的所有可能。只允许加减乘除和括号。
头像
TheMatrix
论坛支柱
论坛支柱
2024年度优秀版主
TheMatrix 的博客
帖子互动: 271
帖子: 13564
注册时间: 2022年 7月 26日 00:35

Re: 熟练写码的版友来解决一个算24问题

帖子 TheMatrix »

FGH 写了: 2023年 1月 21日 23:21 请列出四个不超过10的自然数算24时中间产物必须出现分数的所有可能。只允许加减乘除和括号。
这个我以前写过。(3,3,8,8) 是最难的。好像还有一个类似的。
FGH楼主
论坛精英
论坛精英
帖子互动: 106
帖子: 7032
注册时间: 2022年 7月 25日 16:30

Re: 熟练写码的版友来解决一个算24问题

帖子 FGH楼主 »

TheMatrix 写了: 2023年 1月 21日 23:24 这个我以前写过。(3,3,8,8) 是最难的。好像还有一个类似的。
5551
邢育森(老邢, 邢捕头)
著名写手
著名写手
帖子互动: 12
帖子: 300
注册时间: 2022年 7月 27日 22:50

Re: 熟练写码的版友来解决一个算24问题

帖子 邢育森(老邢, 邢捕头) »

FGH 写了: 2023年 1月 21日 23:435551
(5-(1/5))*5
邢育森(老邢, 邢捕头)
著名写手
著名写手
帖子互动: 12
帖子: 300
注册时间: 2022年 7月 27日 22:50

Re: 熟练写码的版友来解决一个算24问题

帖子 邢育森(老邢, 邢捕头) »

TheMatrix 写了: 2023年 1月 21日 23:24 这个我以前写过。(3,3,8,8) 是最难的。好像还有一个类似的。
这个有解码?
drifter
论坛精英
论坛精英
帖子互动: 457
帖子: 7448
注册时间: 2022年 9月 1日 04:17

Re: 熟练写码的版友来解决一个算24问题

帖子 drifter »

把这些数字扔到Google search就出结果了 自己想破头
drifter
论坛精英
论坛精英
帖子互动: 457
帖子: 7448
注册时间: 2022年 9月 1日 04:17

Re: 熟练写码的版友来解决一个算24问题

帖子 drifter »

可以试试ai能不能解出来
头像
MaLaRabbit
论坛点评
论坛点评
帖子互动: 151
帖子: 2714
注册时间: 2022年 7月 24日 02:16

Re: 熟练写码的版友来解决一个算24问题

帖子 MaLaRabbit »

TheMatrix 写了: 2023年 1月 21日 23:24 这个我以前写过。(3,3,8,8) 是最难的。好像还有一个类似的。
还有一个3737
邢育森(老邢, 邢捕头)
著名写手
著名写手
帖子互动: 12
帖子: 300
注册时间: 2022年 7月 27日 22:50

Re: 熟练写码的版友来解决一个算24问题

帖子 邢育森(老邢, 邢捕头) »

MaLaRabbit 写了: 2023年 1月 22日 00:08 还有一个3737
7*(3+(3/7))
FGH楼主
论坛精英
论坛精英
帖子互动: 106
帖子: 7032
注册时间: 2022年 7月 25日 16:30

Re: 熟练写码的版友来解决一个算24问题

帖子 FGH楼主 »

邢育森 写了: 2023年 1月 21日 23:57 这个有解码?
8/(3-8/3)
头像
verdelite
论坛元老
论坛元老
帖子互动: 1039
帖子: 24253
注册时间: 2022年 7月 21日 23:33

Re: 熟练写码的版友来解决一个算24问题

帖子 verdelite »

FGH 写了: 2023年 1月 21日 23:21 请列出四个不超过10的自然数算24时中间产物必须出现分数的所有可能。只允许加减乘除和括号。
Python code 来了:
from fractions import Fraction
from itertools import permutations
operators=[["(","+",")" ],["(","-",")"],["(","*",")"],["Fraction(",",",")"]]
def ProcessApair(y):
  return([list(x[0:i])+[operators[j][0]+x[ i ]+operators[j][1]+x[i+1]+operators[j][2]]+list(x[i+2:len(x)]) for x in y for i in range(len(x)-1) for j in range(len(operators))])

def evaluate(x):
    try:
        return eval(x)
    except ZeroDivisionError:
        pass
    
y=permutations([str(i) for i in [3,3,7,7]])
results=list(set([item[0] for item in ProcessApair(ProcessApair(ProcessApair(y))) if evaluate(item[0])==24 or evaluate(item[0])==Fraction(24,1)]))
for item in results:
    print(item)
这儿有online python console可以直接跑,
https://www.programiz.com/python-progra ... -compiler/

3,3,7,7的结果:
(7*(3+Fraction(3,7)))
((3+Fraction(3,7))*7)
(7*(Fraction(3,7)+3))
((Fraction(3,7)+3)*7)

3,3,8,8的结果:
Fraction(8,(3-Fraction(8,3)))

1,5,5,5的结果:
(5*(5-Fraction(1,5)))
((5-Fraction(1,5))*5)

2,7,8,10的结果:
解不出来。

1,7,8,10的结果:(里面很多重复的,没想好怎么reduce,LOL。现在只去除了字面的重复。)
((10-(7*1))*8)
(((7+8)-1)+10)
(8+(7+(10-1)))
(((8+10)-1)+7)
((7+8)+(10-1))
((7+(10+8))-1)
((1*8)*(10-7))
(((7-1)+8)+10)
((10+7)+(8-1))
(((10+7)+8)-1)
((10-(1-7))+8)
((8+(10+7))-1)
(8-(1-(10+7)))
((7-1)+(10+8))
(7-(1-(8+10)))
(((7+10)-1)+8)
((10+(8-1))+7)
(((10+8)-1)+7)
((10+8)-(1-7))
(((10*1)-7)*8)
(8-((1-10)-7))
(10-((1-7)-8))
((10-7)*(8*1))
((10+(7+8))-1)
((7-(1-8))+10)
(7+((8-1)+10))
(((8-1)+10)+7)
(((10+7)-1)+8)
((8+(7-1))+10)
(10-((1-8)-7))
((10-1)+(7+8))
((8*1)*(10-7))
(8+((7+10)-1))
((8+(7+10))-1)
(10+(8+(7-1)))
(((8+7)+10)-1)
(8*((10*1)-7))
((8+10)+(7-1))
(8*((1*10)-7))
(10-(1-(8+7)))
((10+(7-1))+8)
((10-(1-8))+7)
(8*((10-7)*1))
(8+((10-1)+7))
(((8+7)-1)+10)
((10+7)-(1-8))
(Fraction(8,1)*(10-7))
(Fraction((10-7),1)*8)
((10-7)*(1*8))
(((1*10)-7)*8)
Fraction(((10-7)*8),1)
((7+(8+10))-1)
(10+(7+(8-1)))
(10+((8+7)-1))
(8+(10-(1-7)))
(8*(1*(10-7)))
((10-7)*Fraction(8,1))
(7+(8-(1-10)))
((8+(10-1))+7)
((10-1)+(8+7))
Fraction((10-7),Fraction(1,8))
((7-1)+(8+10))
(7+(10+(8-1)))
((10+8)+(7-1))
(7-(1-(10+8)))
(7+((10+8)-1))
(7+(10-(1-8)))
Fraction((8*(10-7)),1)
(((10-7)*1)*8)
(8+(10+(7-1)))
((8*(10-7))*1)
(((7-1)+10)+8)
((8-1)+(7+10))
(((7+8)+10)-1)
(8*(10-(7*1)))
(8*(10-(1*7)))
(10+(8-(1-7)))
(8+((10+7)-1))
((1*(10-7))*8)
((7+10)+(8-1))
(((10+8)+7)-1)
(8-((1-7)-10))
(8*(Fraction(10,1)-7))
(7-((1-8)-10))
((8+7)-(1-10))
(8*Fraction((10-7),1))
(10+((8-1)+7))
(8+((7-1)+10))
(((10-1)+8)+7)
(10+(7-(1-8)))
(10+((7-1)+8))
((8+7)+(10-1))
((7-(1-10))+8)
((7+8)-(1-10))
((8-1)+(10+7))
(((10-7)*8)*1)
(10-(1-(7+8)))
((8+10)-(1-7))
(7-((1-10)-8))
((Fraction(10,1)-7)*8)
(1*(8*(10-7)))
(1*((10-7)*8))
(7+((10-1)+8))
(8*(10-Fraction(7,1)))
((7+(10-1))+8)
(((7+10)+8)-1)
(8-(1-(7+10)))
Fraction(8,Fraction(1,(10-7)))
((7+(8-1))+10)
((7+10)-(1-8))
(7+(8+(10-1)))
(7+((8+10)-1))
(10+((7+8)-1))
(((8-1)+7)+10)
((10-Fraction(7,1))*8)
(8+(7-(1-10)))
(((10-1)+7)+8)
((8-(1-7))+10)
(((8+10)+7)-1)
((8-(1-10))+7)
((10-(1*7))*8)
((10+(8+7))-1)
FGH楼主
论坛精英
论坛精英
帖子互动: 106
帖子: 7032
注册时间: 2022年 7月 25日 16:30

Re: 熟练写码的版友来解决一个算24问题

帖子 FGH楼主 »

1,7,8,10显然不是我要的。
verdelite 写了: 2023年 1月 22日 14:41 Python code 来了:
from fractions import Fraction
from itertools import permutations
operators=[["(","+",")" ],["(","-",")"],["(","*",")"],["Fraction(",",",")"]]
def ProcessApair(y):
  return([list(x[0:i])+[operators[j][0]+x[ i ]+operators[j][1]+x[i+1]+operators[j][2]]+list(x[i+2:len(x)]) for x in y for i in range(len(x)-1) for j in range(len(operators))])

def evaluate(x):
    try:
        return eval(x)
    except ZeroDivisionError:
        pass
    
y=permutations([str(i) for i in [3,3,7,7]])
results=list(set([item[0] for item in ProcessApair(ProcessApair(ProcessApair(y))) if evaluate(item[0])==24 or evaluate(item[0])==Fraction(24,1)]))
for item in results:
    print(item)
这儿有online python console可以直接跑,
[url]https://www.programiz.com/python-progra ... -compiler/[\url]

3,3,7,7的结果:
(7*(3+Fraction(3,7)))
((3+Fraction(3,7))*7)
(7*(Fraction(3,7)+3))
((Fraction(3,7)+3)*7)

3,3,8,8的结果:
Fraction(8,(3-Fraction(8,3)))

1,5,5,5的结果:
(5*(5-Fraction(1,5)))
((5-Fraction(1,5))*5)

2,7,8,10的结果:
解不出来。

1,7,8,10的结果:(里面很多重复的,没想好怎么reduce,LOL。现在只去除了字面的重复。)
((10-(7*1))*8)
(((7+8)-1)+10)
(8+(7+(10-1)))
(((8+10)-1)+7)
((7+8)+(10-1))
((7+(10+8))-1)
((1*8)*(10-7))
(((7-1)+8)+10)
((10+7)+(8-1))
(((10+7)+8)-1)
((10-(1-7))+8)
((8+(10+7))-1)
(8-(1-(10+7)))
((7-1)+(10+8))
(7-(1-(8+10)))
(((7+10)-1)+8)
((10+(8-1))+7)
(((10+8)-1)+7)
((10+8)-(1-7))
(((10*1)-7)*8)
(8-((1-10)-7))
(10-((1-7)-8))
((10-7)*(8*1))
((10+(7+8))-1)
((7-(1-8))+10)
(7+((8-1)+10))
(((8-1)+10)+7)
(((10+7)-1)+8)
((8+(7-1))+10)
(10-((1-8)-7))
((10-1)+(7+8))
((8*1)*(10-7))
(8+((7+10)-1))
((8+(7+10))-1)
(10+(8+(7-1)))
(((8+7)+10)-1)
(8*((10*1)-7))
((8+10)+(7-1))
(8*((1*10)-7))
(10-(1-(8+7)))
((10+(7-1))+8)
((10-(1-8))+7)
(8*((10-7)*1))
(8+((10-1)+7))
(((8+7)-1)+10)
((10+7)-(1-8))
(Fraction(8,1)*(10-7))
(Fraction((10-7),1)*8)
((10-7)*(1*8))
(((1*10)-7)*8)
Fraction(((10-7)*8),1)
((7+(8+10))-1)
(10+(7+(8-1)))
(10+((8+7)-1))
(8+(10-(1-7)))
(8*(1*(10-7)))
((10-7)*Fraction(8,1))
(7+(8-(1-10)))
((8+(10-1))+7)
((10-1)+(8+7))
Fraction((10-7),Fraction(1,8))
((7-1)+(8+10))
(7+(10+(8-1)))
((10+8)+(7-1))
(7-(1-(10+8)))
(7+((10+8)-1))
(7+(10-(1-8)))
Fraction((8*(10-7)),1)
(((10-7)*1)*8)
(8+(10+(7-1)))
((8*(10-7))*1)
(((7-1)+10)+8)
((8-1)+(7+10))
(((7+8)+10)-1)
(8*(10-(7*1)))
(8*(10-(1*7)))
(10+(8-(1-7)))
(8+((10+7)-1))
((1*(10-7))*8)
((7+10)+(8-1))
(((10+8)+7)-1)
(8-((1-7)-10))
(8*(Fraction(10,1)-7))
(7-((1-8)-10))
((8+7)-(1-10))
(8*Fraction((10-7),1))
(10+((8-1)+7))
(8+((7-1)+10))
(((10-1)+8)+7)
(10+(7-(1-8)))
(10+((7-1)+8))
((8+7)+(10-1))
((7-(1-10))+8)
((7+8)-(1-10))
((8-1)+(10+7))
(((10-7)*8)*1)
(10-(1-(7+8)))
((8+10)-(1-7))
(7-((1-10)-8))
((Fraction(10,1)-7)*8)
(1*(8*(10-7)))
(1*((10-7)*8))
(7+((10-1)+8))
(8*(10-Fraction(7,1)))
((7+(10-1))+8)
(((7+10)+8)-1)
(8-(1-(7+10)))
Fraction(8,Fraction(1,(10-7)))
((7+(8-1))+10)
((7+10)-(1-8))
(7+(8+(10-1)))
(7+((8+10)-1))
(10+((7+8)-1))
(((8-1)+7)+10)
((10-Fraction(7,1))*8)
(8+(7-(1-10)))
(((10-1)+7)+8)
((8-(1-7))+10)
(((8+10)+7)-1)
((8-(1-10))+7)
((10-(1*7))*8)
((10+(8+7))-1)
回复

回到 “STEM”