這題越推越覺得應該用程式寫,所以就用python寫了一個,為什麼用python,因為真的很方便。
先看看理論上是否可行,10道題,每題4個選項,也就是4的10次冪,2的20次冪,也就是20bit,2.5Byte的資料要遍歷,這個數量級一般計算機妥妥夠用,4Byte以內通常都可以,要是20題就是5Byte就比較吃力了,6Byte就要幾周時間了,8Byte就別想了,當然是遍歷完,要是在前面就出結果了例外。
下面是python原始碼:
ask_all = 0;
ask = [0,0,0,0,0,0,0,0,0,0];
while ask_all < 2**20:
for i in range(10):
ask[i] = ((ask_all >> (2*i)) & 0x03);
ask_all = ask_all + 1;
#ASK2
select = [2,3,0,1]
if select[ask[1]] != ask[4]:
continue
#ASK3
flag = 0;
select = [2,5,1,3]
for i in select:
if (ask[i] == ask[select[ask[2]]]) & (i != select[ask[2]]):
flag = 1;
break;
if flag == 1:
#ASK4
select = [[0,4],[1,6],[0,8],[5,9]]
if ask[select[ask[3]][0]] != ask[select[ask[3]][1]]:
#ASK5
select = [7,3,8,6]
if ask[select[ask[4]]] != ask[4]:
#ASK6
select = [[1,3],[0,5],[2,9],[4,8]]
if (ask[select[ask[5]][0]] != ask[7]) | (ask[select[ask[5]][1]] != ask[7]):
#ASK7
select = [2,1,0,3]
count = [ask.count(0),ask.count(1),ask.count(2),ask.count(3)]
if select[ask[6]] != count.index(min(count)):
#ASK8
select = [6,4,1,9]
if (ask[select[ask[7]]] == (ask[0]+1)) | (ask[select[ask[7]]] == (ask[0]-1)):
#ASK9
select = [5,9,1,8]
if ask[0] == ask[5]:
if ask[select[ask[8]]] == ask[4]:
else:
if ask[select[ask[8]]] != ask[4]:
#ASK10
select = [3,2,4,1]
if select[ask[9]] != (max(count) - min(count)):
print i+1,
print("");
for i in ask:
print chr(ord("A")+i),
結果:
BTW:
這題有點意思,以後可以用來做面試題
這題越推越覺得應該用程式寫,所以就用python寫了一個,為什麼用python,因為真的很方便。
先看看理論上是否可行,10道題,每題4個選項,也就是4的10次冪,2的20次冪,也就是20bit,2.5Byte的資料要遍歷,這個數量級一般計算機妥妥夠用,4Byte以內通常都可以,要是20題就是5Byte就比較吃力了,6Byte就要幾周時間了,8Byte就別想了,當然是遍歷完,要是在前面就出結果了例外。
下面是python原始碼:
ask_all = 0;
ask = [0,0,0,0,0,0,0,0,0,0];
while ask_all < 2**20:
for i in range(10):
ask[i] = ((ask_all >> (2*i)) & 0x03);
ask_all = ask_all + 1;
#ASK2
select = [2,3,0,1]
if select[ask[1]] != ask[4]:
continue
#ASK3
flag = 0;
select = [2,5,1,3]
for i in select:
if (ask[i] == ask[select[ask[2]]]) & (i != select[ask[2]]):
flag = 1;
break;
if flag == 1:
continue
#ASK4
select = [[0,4],[1,6],[0,8],[5,9]]
if ask[select[ask[3]][0]] != ask[select[ask[3]][1]]:
continue
#ASK5
select = [7,3,8,6]
if ask[select[ask[4]]] != ask[4]:
continue
#ASK6
select = [[1,3],[0,5],[2,9],[4,8]]
if (ask[select[ask[5]][0]] != ask[7]) | (ask[select[ask[5]][1]] != ask[7]):
continue
#ASK7
select = [2,1,0,3]
count = [ask.count(0),ask.count(1),ask.count(2),ask.count(3)]
if select[ask[6]] != count.index(min(count)):
continue
#ASK8
select = [6,4,1,9]
if (ask[select[ask[7]]] == (ask[0]+1)) | (ask[select[ask[7]]] == (ask[0]-1)):
continue
#ASK9
select = [5,9,1,8]
if ask[0] == ask[5]:
if ask[select[ask[8]]] == ask[4]:
continue
else:
if ask[select[ask[8]]] != ask[4]:
continue
#ASK10
select = [3,2,4,1]
if select[ask[9]] != (max(count) - min(count)):
continue
for i in range(10):
print i+1,
print("");
for i in ask:
print chr(ord("A")+i),
結果:
BTW:
這題有點意思,以後可以用來做面試題