一个字幕有N个士兵,而字幕想选择3个士兵。第一名士兵发现了多个子集,这些子集的累积Xor小于K,第二个士兵Xor = K,第三个士兵Xor> K。
`from itertools import combinations`
`A=[4]`
`K= 5`
def getXor(A):
xor = 0
for i in A:
xor ^= i
return xor
def getFirstSoldierXOR(A, K):
A.sort()
countLessThanK = 0
for r in range(0, len(A)+1):
for comb in combinations(A, r):
xor = getXor(comb)
if xor < K:
countLessThanK += 1
return(countLessThanK)
def getSecondSoldierXOR(A, K):
A.sort()
countEqualK = 0
for r in range(0, len(A)+1):
for comb in combinations(A, r):
xor = getXor(comb)
if xor == K:
countEqualK += 1
return(countEqualK)
def getThirdSoldierXOR(A, K):
A.sort()
countGreaterK = 0
for r in range(0, len(A)+1):
for comb in combinations(A, r):
xor = getXor(comb)
if xor > K:
countGreaterK += 1
return(countGreaterK)
driver function:
def fun (A, K):
cnt1 = getFirstSoldierXOR(A, K)
cnt2 = getSecondSoldierXOR(A, K)
cnt3 = getThirdSoldierXOR(A, K)
x = cnt1+cnt2
y = cnt2+cnt3
z = cnt3 +cnt1
j = pow(x,2)+ pow(y,2)+ pow(z,2)
sqrt = cnt1**(2)+ cnt2**(2)+cnt3**(2)
return(j-sqrt)
这些是我写给他理解的3个函数,但他说他的测试用例失败了。大家能不能为您提供输入,我在做什么错了?