python 入门
1.确定
def ef(l,r,ans):
while l<=r:
mid=(l+r)//2
if mid>ans:
r=mid-1
elif mid<ans:
l=mid+1
else:
return True
return False
print(ef(1,30,19))
结果
2.随机
import random
def suiji(x,ans):
while x<ans:
m=random.randint(0,1)
if m==1:
x+=1
else :
x-=1
print(x)
return True
print(suiji(0,19))
结果