膜拜下微博 ID:烤串运动员https://weibo.com/u/3986886331的代码
稍微修改了下
def to4(x):
list = []
while x > 3:
list.append(str(x % 4))
x = x // 4
if x:
list.append(str(x))
x = ''.join(reversed(list))
x = x.rjust(10, '0')
return x
def judge(x) -> bool:
minS = min('0123', key=x.count)
maxS = max('0123', key=x.count)
n = [int(i) for i in x]
select2 = '2301'
if select2[n[1]] != x[4]:
return False
select3 = '2513'
temp = select3.replace(select3[n[2]], '')
if x[int(select3[n[2]])] in [x[int(i)] for i in temp]:
return False
select4 = [(0, 4), (1, 6), (0, 8), (5, 9)]
temp = select4[n[3]]
if x[temp[0]] != x[temp[1]]:
return False
select5 = '7386'
if x[int(select5[n[4]])] != x[4]:
return False
select6 = [(1, 3), (0, 5), (2, 9), (4, 8)]
temp = select6[n[5]]
if x[temp[0]] != x[7] or x[temp[1]] != x[7]:
return False
select7 = '2103'
if select7[n[6]] != minS:
return False
select8 = '6419'
if abs(int(select8[n[7]])-int(x[0])) == 1:
return False
select9 = '5918'
temp = x[int(select9[n[8]])] == x[4]
if(x[0] == x[5]) == temp:
return False
select10 = '3241'
if(x.count(maxS) - x.count(minS)) != int(select10[n[9]]):
return False
return True
for x in range(4**10):
x = to4(x)
if judge(x):
print(''.join([chr(65+int(i)) for i in x]))
智商不足,看不懂.
有意思
是真的强。。逻辑慎密,我对着代码看,还是一头雾水。
觉得挺有意思的题目,写不了一楼大神的代码,只能用最笨的做法来写:
根据题干定义好 question1-10。用穷举法 组合出 10 道题的答案组合,然后对每个穷举的组合,和通过题干计算的答案组合是否匹配。
跑一下脚本,结果是第 80009 种组合是正确答案: ['B', 'C', 'A', 'C', 'A', 'C', 'D', 'A', 'B', 'A'] 。答案与一楼大神的结果一致。
def question1():
return newanswerList[0]
def question2():
if newanswerList[4]=='A':
return 'C'
elif newanswerList[4]=='B':
return 'D'
elif newanswerList[4]=='C':
return 'A'
elif newanswerList[4]=='D':
return 'B'
def question3():
list3 = [newanswerList[2],newanswerList[5],newanswerList[1],newanswerList[3]]
answerList = ['A','B','C','D']
i = 0
j = 1
contin = 1
while (4-i) and (3-j) and contin:
if list3[i]==list3[j]:
i +=1
j +=1
elif list3[i]==list3[i+2]:
return answerList[i+1]
else:
return answerList[i]
def question4():
list4 = [newanswerList[0]==newanswerList[4],newanswerList[1]==newanswerList[6],newanswerList[0]==newanswerList[8],newanswerList[5]==newanswerList[9]]
answerList = ['A','B','C','D']
for i in range(4):
if list4[i]:
return answerList[i]
break
def question5():
list5 = [newanswerList[7],newanswerList[3],newanswerList[8],newanswerList[6]]
answerList = ['A','B','C','D']
for i in range(4):
if list5[i]==answerList[i]:
return answerList[i]
break
def question6():
list6 = [[newanswerList[1],newanswerList[3]],[newanswerList[0],newanswerList[5]],[newanswerList[2],newanswerList[9]],[newanswerList[4],newanswerList[8]]]
answerList = ['A','B','C','D']
for i in range(4):
if list6[i][0]==list6[i][0] and list6[i][0]==question8():
return answerList[i]
break
def question7():
list7 = [newanswerList[0],newanswerList[1],newanswerList[2],newanswerList[3],newanswerList[4],newanswerList[5],newanswerList[6],newanswerList[7],newanswerList[8],newanswerList[9]]
answerList = ['A','B','C','D']
countList = []
for i in range(4):
countList.append(list7.count(answerList[i]))
for i in range(4):
if countList[i]==min(countList):
return answerList[i]
def question8():
list8 = [newanswerList[6],newanswerList[4],newanswerList[1],newanswerList[9]]
answerList = ['A','B','C','D']
for i in range(4):
if abs(answerList.index(newanswerList[0])-answerList.index(list8[i]))>1:
return answerList[i]
def question9():
list9 = [newanswerList[5],newanswerList[9],newanswerList[1],newanswerList[8]]
answerList = ['A','B','C','D']
for i in range(4):
if (newanswerList[0]==newanswerList[5])!=(newanswerList[4]==list9[i]):
return answerList[i]
def question10():
list7 = [newanswerList[0],newanswerList[1],newanswerList[2],newanswerList[3],newanswerList[4],newanswerList[5],newanswerList[6],newanswerList[7],newanswerList[8],newanswerList[9]]
answerList = ['A','B','C','D']
list1 = [3,2,4,1]
countList = []
for i in range(4):
countList.append(list7.count(answerList[i]))
diff = max(countList)-min(countList)
if diff in list1:
return answerList[list1.index(diff)]
def compareResult(newanswerList):
respectList = [question1(), question2(), question3(), question4(), question5(), question6(), question7(),
question8(), question9(), question10()]
if None in respectList:
return 0
else:
result = (respectList == newanswerList)
if result:
print('result is correct! ', newanswerList, respectList)
return 1
else:
return 0
resultList = ['A', 'B', 'C', 'D']
answerList = [0,0,0,0,0,0,0,0,0,0]
countA = 0
k = 0
newMod = 0
for i in range(4**10):
count = int(i/4)
modCount = i%4
j = 0
answerList[0] = modCount
while count :
j +=1
answerList[j]=count%4
count = int(count/4)
newanswerList = []
for k in range(10):
newanswerList.append(resultList[answerList[k]])
if(compareResult(newanswerList)):
print(newanswerList,i)
break