有一个有趣的纸牌游戏,我喜欢玩,当我与一群书呆子。Python卡牌游戏
规则很简单:一个人放下四张牌。在这个版本的游戏中,王室数量为10.游戏的重点是使用任何二元运算符来制作24。作为一个例子:
10,2,3,6
(10-6)*(2 * 3)
4 * 6
所以...为什么不用计算机来为我解决这个蛮横的问题?
from math import floor,ceil
for i in range(1000): # not 10,000 yet. It would run too long.
one, two, three, four = i//(1000)+1,(i%1000)//100+1,(i%100)//10+1,i%10+1
operand = [int.__add__,
int.__sub__,
int.__mul__,
int.__truediv__,
int.__pow__,
int.__mod__,
int.__or__, # even though I'd have to be a pedant to use these, I'm still curious
int.__and__,
int.__xor__,
int.__lshift__,
int.__rshift__]
op_str = {int.__add__:"+",
int.__sub__:"-",
int.__mul__:"*",
int.__truediv__:"/",
int.__pow__:"**",
int.__mod__:"%",
int.__or__:"|",
int.__and__:"&",
int.__xor__:"^",
int.__lshift__:"<<",
int.__rshift__:">>"}
for j in range(pow(len(operand),3)):
try:
a = operand[j%len(operand)]
b = operand[(j%(len(operand)**2))//len(operand)]
c = operand[(j%(len(operand)**3))//(len(operand)**2)]
# serial
answer = a(one,b(two,c(three,four)))
if ceil(answer) == floor(answer) and answer == 24:
print(one, op_str[a], "(", two, op_str[b], "(", three, op_str[c], four, "))")
print(one, op_str[a], "(", two, op_str[b], "(", c(three, four), "))")
print(one, op_str[a], "(", b(two,c(three,four)), ")")
print(a(one,b(two,c(three,four))))
continue
# tree
answer = c(a(one, two), b(three, four))
if ceil(answer) == floor(answer) and answer == 24:
print("((", one, op_str[a], two, ")", op_str[b], "(", three, op_str[c], four, "))")
print("(", a(one, two), op_str[b], c(three, four), ")")
print(c(a(one, two), b(three, four)))
continue
except Exception:
pass # I just want to bypass any stupid problems with modulus, divide and the shifts
除此之外,我越来越傻回答:
...
((1 % 1) * (6 | 4))
(0 * 6)
24
((1 - 1) * (6 + 4))
(0 * 10)
24
...
任何人看到这个问题?