Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
level3-lesson16-diy2
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
9a3fb931
authored
Jan 08, 2022
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
auto save
parent
adaf6f8a
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
31 additions
and
0 deletions
222.py
222.py
0 → 100644
View file @
9a3fb931
# 使用二分查找法,找出9和20在列表里面的索引
def
binarySearch
(
alist
,
bingoNum
):
low
=
0
high
=
len
(
alist
)
-
1
while
high
>
low
:
mid
=
(
low
+
high
)
//
2
num
=
alist
[
mid
]
if
bingoNum
==
num
:
return
mid
elif
num
>
bingoNum
:
high
=
mid
-
1
else
:
low
=
mid
+
1
return
None
num_list
=
[
5435
,
423
,
432
,
3
,
43
,
5
,
66
,
547
,
76
,
64
,
654
,
676
,
8
,
565
,
32
,
45
,
4356
,
778
,
56
,
645
,
6
,
345
,
4
,
55
,
54
]
num_list
.
sort
()
print
(
num_list
)
while
True
:
guess
=
input
(
"请输入需要查找的数:"
)
if
guess
==
"q"
:
break
else
:
guess
=
int
(
guess
)
print
(
binarySearch
(
num_list
,
guess
))
print
(
binarySearch
(
num_list
,
guess
))
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