def binsearch(a,c):
first=0
last=len(a)-1
found=False
while first<=last and not found:
mid=(first+last)//2
if(a[mid]==c):
found=True
elif a[mid]<c:
last=mid-1
else:
first=mid+1
return found
a=list()
n=raw_input("Enter how many elements:")
for i in range(int(n)):
num=raw_input("Enter the elements:")
a.append(int(num))
c=raw_input("Enter the element u wanna search:")
b=binsearch(a,c)
if b:
print "Element",c,"found in position."
else:
print "Element not found."
答案 0 :(得分:1)
错误是c
是一个字符串。
只需在c = int(c)
行
c = raw_input("...")
即可