Commit c1c87956 by BellCodeEditor

auto save

parent c19273e3
Showing with 86 additions and 8 deletions
import random
alist = []
for i in range(1,101):
alist.append(i)
num = random.choice(alist)
def binary_search(alist,num):
low = 0
high = len(alist)-1
while low <= high:
mid = (low + high) // 2
guess = alist[mid]
if guess == num:
return mid
elif guess < num:
low = mid + 1
else:
high = mid-1
return None
result = binary_search(alist,num)
print('老师给的数是', num)
print('找到了,他的索引是', result)
\ No newline at end of file
import random import random
def guess():
count = 1
realnumber = random.randint(1,200)
while True:
guessnumber = int(input("输入"))
if guessnumber < realnumber:
count += 1
print("小")
elif guessnumber > realnumber:
print("大")
count += 1
else:
print('你猜对了,猜了',count,'次')
break
def binary_search(alist,item):
low = 0
high = len(alist)-1
while low <= high:
mid = int((low + high)/2)
guess = alist[mid]
if guess > item:
high = mid-1
elif guess < item:
low = mid + 1
else:
return mid
return None
alist = [] if __name__ == "__main__":
for i in range(1, 101): #guess()
alist.append(i) list = [1,2,3,4,5,6,7,8,9,10]
num = random.choice(alist) print(binary_search(list,7))
# 请完善二分查找函数binary_search(),查找出num在列表alist里面的索引位置 \ No newline at end of file
def binary_search(alist,num):
\ No newline at end of file
# a = int(input("人数"))
# if a <= 5:
# y = a*300
# print('要',y,'元')
# else:
# z = a*280
# print('要',z,'元')
# a = int(input("a"))
# b = int(input("b"))
# z=a
# if a<=b:
# print(a," ",b)
# else:
# a=b
# b=z
# print(a," ",b)
# a = int(input())
# if a%4==0 and a%100!=0:
# print("yes")
# elif a%400 == 0:
# print("yes")
# else:
# print("no")
for i in range(0,102,2):
print(i)
\ 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