Python实现全排列的打印
本文为大家分享了Python实现全排列的打印的代码,供大家参考,具体如下
问题:输入一个数字:3,打印它的全排列组合:123132213231312321,并进行统计个数。
下面是Python的实现代码:
#!/usr/bin/envpython #-*-coding:-*- ''' 全排列的demo input:3 output:123132213231312321 ''' total=0 defpermutationCove(startIndex,n,numList): '''递归实现交换其中的两个。一直循环下去,直至startIndex==n ''' globaltotal ifstartIndex>=n: total+=1 printnumList return foriteminrange(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("pleaseinputyournumber:")) startIndex=0 total=0 numList=[xforxinrange(1,n+1)] print'*'*20 foriteminrange(0,n): numList[startIndex],numList[item]=numList[item],numList[startIndex] permutationCove(startIndex+1,n,numList) numList[startIndex],numList[item]=numList[item],numList[startIndex] printtotal
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。
热门推荐
10 喜得孙子祝福语简短唯美
11 婚礼祝福语简短朴实大众
12 员工周年祝福语大全简短
13 家长回复节日祝福语简短
14 简短生日祝福语老同学
15 夜雨祝福语简短10字
16 蛋糕祝福语简短闺蜜
17 部门过年祝福语简短独特
18 简短精炼的结婚祝福语