Python實(shí)現(xiàn)全排列的打印
本文為大家分享了Python實(shí)現(xiàn)全排列的打印的代碼,供大家參考,具體如下
問題:輸入一個(gè)數(shù)字:3,打印它的全排列組合:123 132 213 231 312 321,并進(jìn)行統(tǒng)計(jì)個(gè)數(shù)。
下面是Python的實(shí)現(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): '''遞歸實(shí)現(xiàn)交換其中的兩個(gè)。一直循環(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)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
asyncio 的 coroutine對(duì)象 與 Future對(duì)象使用指南
asyncio是Python 3.4版本引入的標(biāo)準(zhǔn)庫,直接內(nèi)置了對(duì)異步IO的支持。asyncio的編程模型就是一個(gè)消息循環(huán)。今天我們就來詳細(xì)討論下asyncio 中的 coroutine 與 Future對(duì)象2016-09-09Python使用GeoIP2實(shí)現(xiàn)地圖定位
GeoIP2是一種IP地址定位庫,它允許開發(fā)人員根據(jù)IP地址查找有關(guān)位置和地理位置的信息,這篇文章主要為大家介紹了python如何使用GeoIP2實(shí)現(xiàn)地圖定位,感興趣的可以了解下2023-10-10Python數(shù)據(jù)處理之savetxt()和loadtxt()使用詳解
這篇文章主要介紹了Python數(shù)據(jù)處理之savetxt()和loadtxt()使用詳解,NumPy提供了多種存取數(shù)組內(nèi)容的文件操作函數(shù),保存數(shù)組數(shù)據(jù)的文件可以是二進(jìn)制格式或者文本格式,今天我們來看看savetxt()和loadtxt()的用法,需要的朋友可以參考下2023-08-08對(duì)python3 中方法各種參數(shù)和返回值詳解
今天小編就為大家分享一篇對(duì)python3 中方法各種參數(shù)和返回值詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-12-12使用Python快速打開一個(gè)百萬行級(jí)別的超大Excel文件的方法
這篇文章主要介紹了使用Python快速打開一個(gè)百萬行級(jí)別的超大Excel文件的方法,本文通過實(shí)例代碼給大家介紹的非常想詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03