-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
52 lines (48 loc) · 1.04 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import random,time
min1 = 1
max1 = 100
s = random.randint(min1,max1)
num = 0
t = 0.5
start_time = time.time()
def check(a):
global num
num += 1
if a > s:
print(a)
time.sleep(t)
print('太大了')
print('-----')
time.sleep(t)
return 'big'
elif a < s:
print(a)
time.sleep(t)
print('太小了')
print('-----')
time.sleep(t)
return 'small'
elif a == s:
print(a)
time.sleep(t)
print('猜对了')
print('-----')
time.sleep(t)
return 'yes'
def f():
global s,min1,max1,max_num,num,start_time
z = (max1+min1)//2
guess = check(z)
if guess=='small':
min1 = z
f()
elif guess=='big':
max1 = z
f()
elif guess=='yes':
print('good,二分查找完成')
print('s:{},z:{}'.format(s,z))
end_time = time.time()
run_time = end_time - start_time
print('本次耗时:{},猜测次数:{}'.format(run_time,num))
f()