Python实现全排列的打印

作者:enciyetu 时间:2021-09-01 04:08:30 

本文为大家分享了Python实现全排列的打印的代码,供大家参考,具体如下

问题:输入一个数字:3,打印它的全排列组合:123 132 213 231 312 321,并进行统计个数。

下面是Python的实现代码:


#!/usr/bin/env python
# -*- coding: <encoding name> -*-
'''
全排列的demo
input : 3
output: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 = 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

来源:https://blog.csdn.net/enciyetu/article/details/50976026

标签:python,全排列,打印
0
投稿

猜你喜欢

  • PHP的Yii框架的常用日志操作总结

    2023-10-30 23:17:03
  • 网站数据库,是选SQL Server还是Access好

    2008-05-23 13:19:00
  • 五个有趣的Python整蛊小程序合集

    2022-10-27 12:34:10
  • 通过Python中的CGI接口讲解什么是WSGI

    2022-08-10 15:04:24
  • 下拉列表两级连动的新方法(一)

    2009-06-04 18:18:00
  • javascript 版 Bad Apple 字符动画

    2010-01-28 12:19:00
  • border-radius与圆角

    2008-12-29 14:05:00
  • Python操作Jira库常用方法解析

    2022-02-06 01:56:11
  • Python + selenium自动化环境搭建的完整步骤

    2023-11-19 12:42:57
  • 定格动画浅析(一)

    2009-07-30 12:50:00
  • 详解python上传文件和字符到PHP服务器

    2023-10-15 19:02:12
  • Go如何实现HTTP请求限流示例

    2023-07-21 00:40:27
  • 如何解决ORA-01843与NLS_DATE_FORMAT问题

    2023-06-30 20:57:14
  • PHP PDOStatement::errorInfo讲解

    2023-06-04 14:11:53
  • JavaScript事件详细讲解

    2023-08-24 04:51:48
  • 举例讲解Linux系统下Python调用系统Shell的方法

    2023-08-25 00:04:46
  • tensorflow对图像进行拼接的例子

    2022-05-30 02:11:49
  • 常用于后台开发的jQuery插件

    2010-09-25 12:47:00
  • 让所有IE支持HTML5的解决方案

    2009-10-31 18:43:00
  • Python开发编码规范

    2021-04-10 21:07:53
  • asp之家 网络编程 m.aspxhome.com