-
-
Save creamidea/8133272 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# 字典法 | |
global counter | |
counter = 0 | |
def gem(a, k): | |
""" | |
k是用来表明这次递归从那个固定的数开始 | |
""" | |
n = len(a) | |
global counter | |
if k is n: | |
print_result(a) | |
else: | |
for i in range(k, n): | |
counter = counter+1 | |
a[i], a[k] = a[k], a[i] | |
gem(a, k+1) | |
a[k], a[i] = a[i], a[k] | |
def print_result(a): | |
print [i for i in a] | |
if __name__ == '__main__': | |
gem([1,2,3], 0) | |
print counter |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
字典法生成全排列图解。
