Commit 660e221c by BellCodeEditor

auto save

parent 6e9b6e11
Showing with 20 additions and 25 deletions
from tkinter import filedialog, Tk
from
import random import turtle
def tree(le):
alist = [] if le>0:
for i in range(1, 101): t.forward(le)
alist.append(i) t.right(30)
num = random.choice(alist) tree(le-10)
# 请完善二分查找函数binary_search(),查找出num在列表alist里面的索引位置 t.left(60)
tree(le-10)
def binary_search(alist,num): t.right(30)
low=0 t.up()
high=len(alist)-1 t.backward(le)
while low<=high: t.down()
mid=(low+high)//2 t=turtle.Turtle()
guess=alist[mid] t.left(90)
if guess==num: tree(50)
return mid t.hideturtle()
elif guess<num: t.done()
low=mid+1 \ No newline at end of file
elif guess>num:
high=mid-1
return None
a=binary_search(alist,num)
print("随机数是",num)
print("它的索引位置是",a)
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment