所以我试图解决UVa Online Judge的下一个问题:https://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=1415
我已经在Python3中编写了一个程序。我的问题是我不断得到错误答案"当我提交我的程序时。我用不同的输入检查了一百万次,包括来自uDebug的输入。在每种情况下,我都得到与预期完全相同的输出。这是非常令人沮丧的,因为我认为这可能是格式错误,因为我知道法官非常挑剔。但是,我无法用我的代码找到问题。如果有人可以帮忙,我将不胜感激。我的代码:
import math
#Binary_search (recursive)
def binary_search(the_array, the_key, imin, imax):
if (imax < imin):
return None
else:
imid = imin + ((imax - imin) / 2)
imid = math.floor(imid)
if the_array[imid] > the_key:
return binary_search(the_array, the_key, imin, imid-1)
elif the_array[imid] < the_key:
return binary_search(the_array, the_key, imid+1, imax)
else:
return imid
#problem solution
case = 1
while True:
canicas = [ ]
queries = [ ]
N, Q = input().split(' ')
N = int(N)
Q= int(Q)
if(N == 0 and Q == 0):
break
else:
for i in range(N):
x = int(input())
canicas.append(x)
for j in range(Q):
x = int(input())
queries.append(x)
canicas.sort()
print ("CASE# %d:" % case)
for q in queries:
pos = binary_search(canicas, q, 0, N-1)
if(pos != None):
if(pos == 0):
print("%d found at 1" % q)
break
if(canicas[pos-1] == q):
print("%d found at %d" % (q, pos))
else:
print("%d found at %d" % (q, pos+1))
else:
print("%d not found" % q)
case += 1