按照顺序生成全排列——基于python切片
1 def total_arrange(base_list: list, length: int, cur_list: list): 2 if length == 0: 3 print("".join(list(map(str, cur_list)))) 4 return 5 for i in range(length): 6 cur_list.append(base_list[i]) 7 total_arrange(base_list[0:i] + base_list[i+1:], length-1, cur_list) 8 cur_list.pop(-1) 9 10 def func(): 13 n = int(input().strip()) 14 15 base_list = list(range(1, n+1)) 16 total_arrange(base_list, n, []) 17 18 19 if __name__ == "__main__": 20 func()