国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Python > 正文

Python實(shí)現(xiàn)全排列的打印

2020-01-04 14:41:29
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文為大家分享了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> -*- '''全排列的demoinput : 3output: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 = 0total = 0numList = [x for x in range(1,n+1)]print '*' * 20for 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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到python教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 交口县| 洪湖市| 延安市| 万年县| 海晏县| 邵武市| 长沙县| 泾阳县| 阿拉善右旗| 志丹县| 通化县| 绥棱县| 遂昌县| 永安市| 上虞市| 花莲市| 赞皇县| 扎赉特旗| 靖宇县| 两当县| 公主岭市| 桂平市| 临夏县| 铁力市| 三河市| 德兴市| 资兴市| 吴江市| 股票| 鱼台县| 阳江市| 天津市| 六盘水市| 贡嘎县| 浏阳市| 太保市| 咸宁市| 民和| 桃源县| 济南市| 敦煌市|