输入整数n(3<=n<=7),编写程序输出1,2,...,n
整数的全排列,按字典序输出。
输入格式:
一行输入正整数n。
输出格式:
按字典序输出1到n的全排列。每种排列占一行,数字间无空格。
输入样例:
在这里给出一组输入。例如:
3
输出样例:
在这里给出相应的输出。例如:
123
132
213
231
312
321
【Python参考代码】
#By yangbo 2020.08.10
import random
n = eval(input())
lst,cnt= set(),1
st = [str(i) for i in range(1,n+1)]
for i in range(1,n+1):
cnt *= i #cnt就是总的可能数,n的阶乘
while len(lst)<cnt:
random.shuffle(st)#打乱列表里面数的顺序
lst.add("".join(st))#用join将数连接
for i in sorted(lst):#排序,字典序
print(i)
版本二
来自:@weixin_48863006:
#By yangbo 2020.08.10
n = int(input())
start = 10**(n-1)
end = int(str(n)*n)
list1 = [str(i) for i in range(1,n+1)]
for i in range(start,end+1):
if len(set(str(i)))==len(str(i)) and sorted(list(str(i)))==list1:
print(i)