梨骑士吧 关注:474贴子:43,214
  • 0回复贴,共1
# -*- coding: utf-8 -*-
from random import randint
import itertools
La = []
n = 1
while n <= 4:
x = randint(1, 10)
La.append(x)
n += 1
print(La)
input('查看计算方法')
Lb = sorted(La)
def twentyfour(cards):
for nums in itertools.permutations(cards):
for ops in itertools.product('+-*/', repeat=3):
bds1 = '({0}{4}{1}){5}({2}{6}{3})'.format(*nums, *ops) # (a+b)*(c-d)
bds2 = '(({0}{4}{1}){5}{2}){6}{3}'.format(*nums, *ops) # (a+b)*c-d
bds3 = '{0}{4}({1}{5}({2}{6}{3}))'.format(*nums, *ops) # a/(b-(c/d))
for bds in [bds1, bds2, bds3]:
try:
if abs(eval(bds) - 24.0) < 1e-10:
return bds
except ZeroDivisionError:
continue
return 'Not found!'
print(twentyfour(Lb))


IP属地:上海1楼2019-07-18 16:31回复