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

首頁 > 編程 > Python > 正文

Python實現全排列的打印

2020-02-15 22:47:22
字體:
來源:轉載
供稿:網友

本文為大家分享了Python實現全排列的打印的代碼,供大家參考,具體如下

問題:輸入一個數字:3,打印它的全排列組合:123 132 213 231 312 321,并進行統計個數。

下面是Python的實現代碼:

#!/usr/bin/env python# -*- coding: <encoding name> -*- '''全排列的demoinput : 3output:123 132 213 231 312 321''' total = 0 def permutationCove(startIndex, n, numList):  '''遞歸實現交換其中的兩個。一直循環下去,直至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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林站長站。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 福州市| 宝鸡市| 社会| 登封市| 宕昌县| 甘孜县| 德保县| 遵义县| 泸溪县| 涞源县| 厦门市| 姜堰市| 万年县| 洪江市| 石渠县| 深圳市| 阜新市| 南乐县| 龙岩市| 肇源县| 晋宁县| 正阳县| 汉沽区| 宜章县| 营口市| 榆树市| 韶关市| 宜城市| 青铜峡市| 寿光市| 泰顺县| 海口市| 手游| 张家界市| 湖口县| 张掖市| 鹤岗市| 固始县| 高碑店市| 辉南县| 平武县|