Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
level3-lesson16-diy1
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
dab52de5
authored
Mar 18, 2022
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
save project
parent
02020452
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
17 additions
and
4 deletions
binary_search.py
binary_search.py
View file @
dab52de5
from
random
import
randint
,
choice
# 使用二分查找法,找出9和20在列表里面的索引
# 使用二分查找法,找出9和20在列表里面的索引
num_list
=
[
1
,
3
,
5
,
8
,
9
,
11
,
15
,
17
,
18
,
20
,
21
,
36
,
44
,
55
,
57
,
67
,
74
,
76
,
84
,
91
,
93
,
98
,
100
]
num_list
=
[
1
,
3
,
5
,
8
,
9
,
11
,
15
,
17
,
18
,
20
,
21
,
36
,
44
,
55
,
57
,
67
,
74
,
76
,
84
,
91
,
93
,
98
,
100
]
index
=
0
for
i
in
range
(
101
,
175
,
randint
(
0
,
4
)):
num_list
.
append
(
i
)
index
=
0
bingo_num
=
[
9
,
20
,
55
,
76
,
98
]
bingo_num
=
[
9
,
20
,
55
,
76
,
98
]
for
i
in
range
(
3
):
bingo_num
.
append
(
choice
(
num_list
))
low
,
high
=
0
,
len
(
num_list
)
-
1
low
,
high
=
0
,
len
(
num_list
)
-
1
def
reast
():
'''
reast low and high.
'''
global
low
,
high
low
,
high
=
0
,
len
(
num_list
)
-
1
while
index
<
len
(
bingo_num
):
while
index
<
len
(
bingo_num
):
low
,
high
=
0
,
len
(
num_list
)
-
1
reast
()
while
low
<=
high
:
while
low
<=
high
:
mid
=
(
low
+
high
)
mid
=
(
low
+
high
)
guess_num
=
num_list
[
mid
]
guess_num
=
num_list
[
mid
]
...
@@ -18,4 +30,5 @@ while index < len(bingo_num):
...
@@ -18,4 +30,5 @@ while index < len(bingo_num):
low
=
mid
+
1
low
=
mid
+
1
elif
guess_num
>
bingo_num
[
index
]:
elif
guess_num
>
bingo_num
[
index
]:
high
=
mid
-
1
high
=
mid
-
1
index
+=
1
index
+=
1
\ No newline at end of file
print
(
"列表是:
%
a,要找的数是:
%
s"
%
(
num_list
,
str
(
bingo_num
)))
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment