最近很火的一道题目,用笔画了很久都找不到其中的联系,无奈使用最笨的穷举法,题目如下:
Python运算:
import time
moc = ["", "", "", "", "", "", "", "", "", ""]
def mach_question():
times = 0
start = time.time()
for i in ["A", "B", "C", "D"]:
# 推测第一道
moc[0] = i
# 推测第二道
for j in ["A", "B", "C", "D"]:
moc[1] = j
# 推测第三道题
for k in ["A", "B", "C", "D"]:
moc[2] = k
# 推测第四道
for l in ["A", "B", "C", "D"]:
moc[3] = l
# 推测第五到
for m in ["A", "B", "C", "D"]:
moc[4] = m
# 推测第六道
for n in ["A", "B", "C", "D"]:
moc[5] = n
## 推测第七道
for q in ["A", "B", "C", "D"]:
moc[6] = q
if m_3(moc):
## 推测第八道
for p in ["A", "B", "C", "D"]:
moc[7] = p
## 推测第九道
for b in ["A", "B", "C", "D"]:
moc[8] = b
## 推算第十项
for c in ["A", "B", "C", "D"]:
moc[9] = c
times = times + 1
if m_5(moc):
if m_4(moc):
if m_6(moc) and m_7(moc) and m_9(moc):
print('匹配:%d次' % times)
print(moc)
break
else:
break
else:
break
else:
break
end = time.time()
print('运行时间:%ss' % (end - start))
## 运算第三道的方法
def m_3(item):
k = item[2]
if k == 'A':
if item[5] != 'A' and item[5] == item[1] == item[3]:
return True
##如果第三道题为B
if k == 'B':
if item[5] != 'B' and item[1] == item[3] == 'B':
return True
##如果第三道题为C
if k == 'C':
if item[1] != 'C' and item[5] == item[3] == 'C':
return True
##如果第三道题为D
if k == 'D':
if item[3] != 'D' and item[5] == item[1] == 'D':
return True
return False
def m_4(item):
n = item[3]
if n == 'A':
if item[0] == item[4] and item[1] != item[6] and item[0] != item[8] and item[5] != item[9]:
return True
if n == 'B':
if item[1] == item[6] and item[0] != item[4] and item[0] != item[8] and item[5] != item[9]:
return True
if n == 'C':
if item[0] == item[8] and item[1] != item[6] and item[0] != item[4] and item[5] != item[9]:
return True
if n == 'D':
if item[5] == item[9] and item[1] != item[6] and item[0] != item[8] and item[0] != item[4]:
return True
return False
## 运算符合第五道的方法
def m_5(item):
m = item[4]
if m == 'A' and item[1] == 'C' and item[7] == 'A' and item[3] != 'A' and item[8] != 'A' and item[6] != 'A':
return True
if m == 'B' and item[1] == 'D' and item[3] == 'B' and item[7] != 'B' and item[8] != 'B' and item[6] != 'B':
return True
if m == 'C' and item[1] == 'A' and item[8] == 'C' and item[7] != 'C' and item[3] != 'C' and item[6] != 'C':
return True
if m == 'D' and item[1] == 'B' and item[6] == 'D' and item[7] != 'D' and item[3] != 'D' and item[8] != 'D':
return True
return False
## 运算第六道
def m_6(item):
j = item[5]
if j == 'A':
if item[1] == item[3] == item[7]:
return True
if j == 'B':
if item[0] == item[5] == item[7]:
return True
if j == 'C':
if item[2] == item[9] == item[7]:
return True
if j == 'D':
if item[4] == item[8] == item[7]:
return True
return False
## 匹配第七道题
def m_7(item):
n = item[7]
one = ord(item[0])
if n == 'A':
return abs(one - ord(item[6])) > 1
if n == 'B':
return abs(one - ord(item[4])) > 1
if n == 'C':
return abs(one - ord(item[1])) > 1
if n == 'D':
return abs(one - ord(item[9])) > 1
##匹配第九道题
def m_9(item):
q = item[8]
if item[0] == item[6]:
if q == 'A':
return item[4] != item[5]
if q == 'B':
return item[4] != item[9]
if q == 'C':
return item[4] != item[1]
if q == 'D':
return item[4] != item[8]
else:
if q == 'A':
return item[4] == item[5]
if q == 'B':
return item[4] == item[9]
if q == 'C':
return item[4] == item[1]
if q == 'D':
return item[4] == item[8]
mach_question()
运算结果如下:
匹配:4658次
['B', 'C', 'A', 'C', 'A', 'C', 'D', 'A', 'B', 'A']
运行时间:0.011355876922607422s