Python實現(xiàn)全排列的打印
更新時間:2018年08月18日 10:12:57 作者:enciyetu
這篇文章主要為大家詳介紹了Python實現(xiàn)全排列的打印的相關(guān)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文為大家分享了Python實現(xiàn)全排列的打印的代碼,供大家參考,具體如下
問題:輸入一個數(shù)字:3,打印它的全排列組合:123 132 213 231 312 321,并進(jìn)行統(tǒng)計個數(shù)。
下面是Python的實現(xiàn)代碼:
#!/usr/bin/env python
# -*- coding: <encoding name> -*-
'''
全排列的demo
input : 3
output:123 132 213 231 312 321
'''
total = 0
def permutationCove(startIndex, n, numList):
'''遞歸實現(xiàn)交換其中的兩個。一直循環(huán)下去,直至startIndex == n
'''
global total
if startIndex >= n:
total += 1
print numList
return
for item in range(startIndex, n):
numList[startIndex], numList[item] = numList[item], numList[startIndex]
permutationCove(startIndex + 1, n, numList )
numList[startIndex], numList[item] = numList[item], numList[startIndex]
n = int(raw_input("please input your number:"))
startIndex = 0
total = 0
numList = [x for x in range(1,n+1)]
print '*' * 20
for item in range(0, n):
numList[startIndex], numList[item] = numList[item], numList[startIndex]
permutationCove(startIndex + 1, n, numList)
numList[startIndex], numList[item] = numList[item], numList[startIndex]
print total
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
asyncio 的 coroutine對象 與 Future對象使用指南
asyncio是Python 3.4版本引入的標(biāo)準(zhǔn)庫,直接內(nèi)置了對異步IO的支持。asyncio的編程模型就是一個消息循環(huán)。今天我們就來詳細(xì)討論下asyncio 中的 coroutine 與 Future對象2016-09-09
Python數(shù)據(jù)處理之savetxt()和loadtxt()使用詳解
這篇文章主要介紹了Python數(shù)據(jù)處理之savetxt()和loadtxt()使用詳解,NumPy提供了多種存取數(shù)組內(nèi)容的文件操作函數(shù),保存數(shù)組數(shù)據(jù)的文件可以是二進(jìn)制格式或者文本格式,今天我們來看看savetxt()和loadtxt()的用法,需要的朋友可以參考下2023-08-08
使用Python快速打開一個百萬行級別的超大Excel文件的方法
這篇文章主要介紹了使用Python快速打開一個百萬行級別的超大Excel文件的方法,本文通過實例代碼給大家介紹的非常想詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03

