互换的青春
尝试这个:n = int(input())for i in range(1, (n//2)+2): for j in range(i): print(i + ((n - 1) * j) - (j * (j - 1)) , end=' ') print()for i in range((n//2)+2, n + 1): for j in range(n - i + 1): print(i + ((n - 1) * j) - (j * (j - 1)), end=' ') print()输出:n = 91 2 10 3 11 17 4 12 18 22 5 13 19 23 25 6 14 20 24 7 15 21 8 16 9 逻辑n = 91 |2 10 |(10 - 2) = 83 11 17 |(11 - 3) = 8, (17 - 11) = 64 12 18 22 |(12 - 4) = 8, (18 - 12) = 6, (22 - 18) = 45 13 19 23 25 |(13 - 5) = 8, (19 - 13) = 6, (23 - 19) = 4, (25 - 23) = 26 14 20 24 |(14 - 6) = 8, (20 - 14) = 6, (24 - 20) = 47 15 21 |(15 - 7) = 8, (21 - 15) = 68 16 |(16 - 8) = 89 |例如在第 5 行第一个数字是5 + ((n - 1) * 0) - (0 * -1) = 5下一个值5 + ((n - 1) * 1) - (1 * 0) = 13下一个值5 + ((n - 1) * 2) - (1 * 2) = 19下一个值5 + ((n - 1) * 3) - (2 * 3) = 23下一个值5 + ((n - 1) * 4) - (3 * 4) = 255 以上是i值,0、1、2、3、4 是j值计算价值的通用公式i + ((n - 1) * j) - (j * (j - 1))