```python import time from itertools import permutations def print_permutations(n): # 生成1到n的列表 numbers = list(range(1, n + 1)) # 使用permutations函数生成全排列 perms = permutations(numbers) # 按字典序输出每个排列 for perm in perms: print(''.join(map(str, perm))) def test_runtime(): for n in range(1, 10): start_time = time.time() print(f"n={n}:") print_permutations(n) end_time = time.time() print(f"运行时间: {end_time - start_time:.6f}秒\n") # 运行测试用例 test_runtime() ``` ### 额外需要注意的逻辑点: 1. 当n值增大时,全排列的数量会以阶乘速度增长,这可能导致程序运行时间显著增加。 2. 使用`itertools.permutations`可以简化全排列的生成过程,但需注意它返回的是一个迭代器,适用于大范围数据处理。 3. 输出时,通过`''.join(map(str, perm))`将数字列表转换成字符串,确保输出格式符合要求。 [2024-11-29 14:19:39 | AI写代码神器 | 259点数解答]