rosalind练习题十九

# Problemrosalind练习题十八

# A permutation of length n is an ordering of the positive integers {1,2,…,n}. For example, π=(5,3,2,1,4) is a permutation of length 5.

# Given: A positive integer n≤7.

# Return: The total number of permutations of length n, followed by a list of all such permutations (in any order).

# Sample Dataset

# 3

# Sample Output

# 6

# 1 2 3

# 1 3 2

# 2 1 3

# 2 3 1

# 3 1 2

# 3 2 1

# 这个题目的目的是输出长度为n的排列的个数以及所有排列。

import itertools

n = 3 # 读取输入n

perms = list(itertools.permutations(range(1, n+1))) # 求出所有排列

print(len(perms)) # 输出排列个数

for perm in perms: # 输出每个排列

    print(" ".join(str(x) for x in perm))

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容