版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
题目描述
Write a program to list all the permutations that can be formed by N integers from 1 to N, and output them in lexicographic order.
For example, given an integer N = 2,
the permutations are:
1 2
2 1
输入
The input is a set of integers, each line is an integer N, N < 8, and the last line is 0.
输出
For each input N, all the permutations of 1 to N are output in lexicographic order,
the numbers are separated by spaces, and the output of each permutation is in one line.
样例输入 Copy
2
3
0
样例输出 Copy
1 2
2 1
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
import itertools
while True:
n=int(input())
s=[]
for i in range(1,n+1):
s.append(i)
list2=list(itertools.permutations(s,n))
for i in list2:
print(*i)