Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
lesson9_2
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
ec92b00a
authored
Aug 14, 2021
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
auto save
parent
5ab88987
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
43 additions
and
0 deletions
diy1.py
diy2.py
diy1.py
0 → 100644
View file @
ec92b00a
'''求[1,2,3,4]的全排列'''
n
=
4
x
=
[
1
,
2
,
3
,
4
]
# 一个解
X
=
[]
# 一组解
# 冲突检测:无
def
conflict
(
k
):
global
n
,
x
,
X
return
False
# 无冲突
# 一个例子
# 冲突检测:元素奇偶相间的排列
def
conflict2
(
k
):
global
n
,
x
,
X
if
k
==
0
:
# 第一个元素,肯定无冲突
return
False
if
x
[
k
-
1
]
%
2
==
x
[
k
]
%
2
:
# 只比较 x[k] 与 x[k-1] 奇偶是否相同
return
True
return
False
# 无冲突
# 排列树递归模板
def
backkrak
(
k
):
# 到达第k个位置
global
n
,
x
,
X
if
k
>=
n
:
# 超出最尾的位置
print
(
x
)
#X.append(x[:]) # 注意x[:]
else
:
for
i
in
range
(
k
,
n
):
# 遍历后面第 k~n-1 的位置
x
[
k
],
x
[
i
]
=
x
[
i
],
x
[
k
]
if
not
conflict2
(
k
):
# 剪枝
backkrak
(
k
+
1
)
x
[
i
],
x
[
k
]
=
x
[
k
],
x
[
i
]
# 回溯
# 测试
backkrak
(
0
)
\ No newline at end of file
diy2.py
View file @
ec92b00a
...
...
@@ -3,3 +3,5 @@ score = {'语文':91,'数学':88,'英语':85}
# 请对字典进行遍历,将保存的分数以键值对的形式提取打印出来
for
k
,
v
in
score
.
items
():
print
(
k
,
v
)
\ 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